home *** CD-ROM | disk | FTP | other *** search
open in:
MacOS 8.1
|
Win98
|
DOS
browse contents |
view JSON data
|
view as text
This file was processed as: LaTeX Document
(document/latex).
Confidence | Program | Detection | Match Type | Support
|
---|
100%
| dexvert
| LaTeX Document (document/latex)
| magic
| Supported |
100%
| dexvert
| Texinfo Document (document/texInfo)
| magic
| Supported |
1%
| dexvert
| Corel 10 Texture (image/corel10Texture)
| ext
| Unsupported |
1%
| dexvert
| Text File (text/txt)
| fallback
| Supported |
100%
| file
| LaTeX document text
| default
| |
99%
| file
| TeX document text
| default
| |
98%
| file
| C source, ASCII text
| default
| |
100%
| checkBytes
| Printable ASCII
| default
| |
100%
| perlTextCheck
| Likely Text (Perl)
| default
| |
100%
| siegfried
| fmt/281 LaTeX (Subdocument)
| default
| |
100%
| detectItEasy
| Format: Plain text[LF]
| default
| |
100%
| xdgMime
| text/x-matlab
| default (weak)
|
|
hex view+--------+-------------------------+-------------------------+--------+--------+
|00000000| 25 25 5c 69 6e 70 75 74 | 7b 65 70 73 66 2e 74 65 |%%\input|{epsf.te|
|00000010| 78 7d 0a 5c 63 68 61 70 | 74 65 72 7b 5c 6c 61 62 |x}.\chap|ter{\lab|
|00000020| 65 6c 7b 73 63 6f 74 74 | 7d 4c 69 6e 75 78 20 49 |el{scott|}Linux I|
|00000030| 6e 74 65 72 70 72 6f 63 | 65 73 73 20 43 6f 6d 6d |nterproc|ess Comm|
|00000040| 75 6e 69 63 61 74 69 6f | 6e 73 7d 0a 5c 61 75 74 |unicatio|ns}.\aut|
|00000050| 68 6f 72 7b 42 2e 20 53 | 63 6f 74 74 20 42 75 72 |hor{B. S|cott Bur|
|00000060| 6b 65 74 74 2c 20 7b 5c | 74 74 20 73 63 6f 74 74 |kett, {\|tt scott|
|00000070| 62 40 69 6e 74 6e 65 74 | 2e 6e 65 74 7d 7d 0a 5c |b@intnet|.net}}.\|
|00000080| 64 61 74 65 7b 76 31 2e | 30 2c 20 32 39 20 4d 61 |date{v1.|0, 29 Ma|
|00000090| 72 63 68 20 31 39 39 35 | 7d 0a 5c 61 62 73 74 72 |rch 1995|}.\abstr|
|000000a0| 61 63 74 7b 41 20 64 65 | 74 61 69 6c 65 64 20 6f |act{A de|tailed o|
|000000b0| 76 65 72 76 69 65 77 20 | 6f 66 20 74 68 65 20 49 |verview |of the I|
|000000c0| 50 43 20 28 69 6e 74 65 | 72 70 72 6f 63 65 73 73 |PC (inte|rprocess|
|000000d0| 20 63 6f 6d 6d 75 6e 69 | 63 61 74 69 6f 6e 20 66 | communi|cation f|
|000000e0| 61 63 69 6c 69 74 69 65 | 73 29 20 66 61 63 69 6c |acilitie|s) facil|
|000000f0| 69 74 69 65 73 0a 69 6d | 70 6c 65 6d 65 6e 74 65 |ities.im|plemente|
|00000100| 64 20 69 6e 20 74 68 65 | 20 4c 69 6e 75 78 20 4f |d in the| Linux O|
|00000110| 70 65 72 61 74 69 6e 67 | 20 53 79 73 74 65 6d 2e |perating| System.|
|00000120| 7d 0a 0a 0a 5c 73 65 63 | 74 69 6f 6e 7b 49 6e 74 |}...\sec|tion{Int|
|00000130| 72 6f 64 75 63 74 69 6f | 6e 7d 0a 0a 54 68 65 20 |roductio|n}..The |
|00000140| 4c 69 6e 75 78 20 49 50 | 43 20 28 49 6e 74 65 72 |Linux IP|C (Inter|
|00000150| 2d 70 72 6f 63 65 73 73 | 20 63 6f 6d 6d 75 6e 69 |-process| communi|
|00000160| 63 61 74 69 6f 6e 29 20 | 66 61 63 69 6c 69 74 69 |cation) |faciliti|
|00000170| 65 73 20 70 72 6f 76 69 | 64 65 20 61 20 6d 65 74 |es provi|de a met|
|00000180| 68 6f 64 20 66 6f 72 0a | 6d 75 6c 74 69 70 6c 65 |hod for.|multiple|
|00000190| 20 70 72 6f 63 65 73 73 | 65 73 20 74 6f 20 63 6f | process|es to co|
|000001a0| 6d 6d 75 6e 69 63 61 74 | 65 20 77 69 74 68 20 6f |mmunicat|e with o|
|000001b0| 6e 65 20 61 6e 6f 74 68 | 65 72 2e 20 20 54 68 65 |ne anoth|er. The|
|000001c0| 72 65 20 61 72 65 20 73 | 65 76 65 72 61 6c 0a 6d |re are s|everal.m|
|000001d0| 65 74 68 6f 64 73 20 6f | 66 20 49 50 43 20 61 76 |ethods o|f IPC av|
|000001e0| 61 69 6c 61 62 6c 65 20 | 74 6f 20 4c 69 6e 75 78 |ailable |to Linux|
|000001f0| 20 43 20 70 72 6f 67 72 | 61 6d 6d 65 72 73 3a 0a | C progr|ammers:.|
|00000200| 5c 62 65 67 69 6e 7b 69 | 74 65 6d 69 7a 65 7d 0a |\begin{i|temize}.|
|00000210| 5c 69 74 65 6d 20 48 61 | 6c 66 2d 64 75 70 6c 65 |\item Ha|lf-duple|
|00000220| 78 20 55 4e 49 58 20 70 | 69 70 65 73 0a 5c 69 74 |x UNIX p|ipes.\it|
|00000230| 65 6d 20 46 49 46 4f 73 | 20 28 6e 61 6d 65 64 20 |em FIFOs| (named |
|00000240| 70 69 70 65 73 29 0a 5c | 69 74 65 6d 20 53 59 53 |pipes).\|item SYS|
|00000250| 56 20 73 74 79 6c 65 20 | 6d 65 73 73 61 67 65 20 |V style |message |
|00000260| 71 75 65 75 65 73 0a 5c | 69 74 65 6d 20 53 59 53 |queues.\|item SYS|
|00000270| 56 20 73 74 79 6c 65 20 | 73 65 6d 61 70 68 6f 72 |V style |semaphor|
|00000280| 65 20 73 65 74 73 0a 5c | 69 74 65 6d 20 53 59 53 |e sets.\|item SYS|
|00000290| 56 20 73 74 79 6c 65 20 | 73 68 61 72 65 64 20 6d |V style |shared m|
|000002a0| 65 6d 6f 72 79 20 73 65 | 67 6d 65 6e 74 73 0a 5c |emory se|gments.\|
|000002b0| 69 74 65 6d 20 4e 65 74 | 77 6f 72 6b 69 6e 67 20 |item Net|working |
|000002c0| 73 6f 63 6b 65 74 73 20 | 28 42 65 72 6b 65 6c 65 |sockets |(Berkele|
|000002d0| 79 20 73 74 79 6c 65 29 | 20 28 6e 6f 74 20 63 6f |y style)| (not co|
|000002e0| 76 65 72 65 64 20 69 6e | 20 74 68 69 73 20 70 61 |vered in| this pa|
|000002f0| 70 65 72 29 0a 5c 69 74 | 65 6d 20 46 75 6c 6c 2d |per).\it|em Full-|
|00000300| 64 75 70 6c 65 78 20 70 | 69 70 65 73 20 28 53 54 |duplex p|ipes (ST|
|00000310| 52 45 41 4d 53 20 70 69 | 70 65 73 29 20 28 6e 6f |REAMS pi|pes) (no|
|00000320| 74 20 63 6f 76 65 72 65 | 64 20 69 6e 20 74 68 69 |t covere|d in thi|
|00000330| 73 20 70 61 70 65 72 29 | 0a 5c 65 6e 64 7b 69 74 |s paper)|.\end{it|
|00000340| 65 6d 69 7a 65 7d 0a 0a | 54 68 65 73 65 20 66 61 |emize}..|These fa|
|00000350| 63 69 6c 69 74 69 65 73 | 2c 20 77 68 65 6e 20 75 |cilities|, when u|
|00000360| 73 65 64 20 65 66 66 65 | 63 74 69 76 65 6c 79 2c |sed effe|ctively,|
|00000370| 20 70 72 6f 76 69 64 65 | 20 61 20 73 6f 6c 69 64 | provide| a solid|
|00000380| 20 66 72 61 6d 65 77 6f | 72 6b 20 66 6f 72 0a 63 | framewo|rk for.c|
|00000390| 6c 69 65 6e 74 2f 73 65 | 72 76 65 72 20 64 65 76 |lient/se|rver dev|
|000003a0| 65 6c 6f 70 6d 65 6e 74 | 20 6f 6e 20 61 6e 79 20 |elopment| on any |
|000003b0| 55 4e 49 58 20 73 79 73 | 74 65 6d 20 28 69 6e 63 |UNIX sys|tem (inc|
|000003c0| 6c 75 64 69 6e 67 20 4c | 69 6e 75 78 29 2e 0a 0a |luding L|inux)...|
|000003d0| 0a 0a 0a 5c 73 65 63 74 | 69 6f 6e 7b 48 61 6c 66 |...\sect|ion{Half|
|000003e0| 2d 64 75 70 6c 65 78 20 | 55 4e 49 58 20 50 69 70 |-duplex |UNIX Pip|
|000003f0| 65 73 7d 0a 0a 0a 0a 0a | 5c 73 75 62 73 65 63 74 |es}.....|\subsect|
|00000400| 69 6f 6e 7b 42 61 73 69 | 63 20 43 6f 6e 63 65 70 |ion{Basi|c Concep|
|00000410| 74 73 7d 0a 0a 53 69 6d | 70 6c 79 20 70 75 74 2c |ts}..Sim|ply put,|
|00000420| 20 61 20 7b 5c 65 6d 20 | 70 69 70 65 5c 2f 7d 20 | a {\em |pipe\/} |
|00000430| 69 73 20 61 20 6d 65 74 | 68 6f 64 20 6f 66 20 63 |is a met|hod of c|
|00000440| 6f 6e 6e 65 63 74 69 6e | 67 20 74 68 65 20 7b 5c |onnectin|g the {\|
|00000450| 65 6d 20 73 74 61 6e 64 | 61 72 64 20 6f 75 74 70 |em stand|ard outp|
|00000460| 75 74 5c 2f 7d 20 6f 66 | 20 6f 6e 65 0a 70 72 6f |ut\/} of| one.pro|
|00000470| 63 65 73 73 20 74 6f 20 | 74 68 65 20 7b 5c 65 6d |cess to |the {\em|
|00000480| 20 73 74 61 6e 64 61 72 | 64 20 69 6e 70 75 74 5c | standar|d input\|
|00000490| 2f 7d 20 6f 66 20 61 6e | 6f 74 68 65 72 2e 20 20 |/} of an|other. |
|000004a0| 50 69 70 65 73 20 61 72 | 65 20 74 68 65 20 65 6c |Pipes ar|e the el|
|000004b0| 64 65 73 74 20 6f 66 20 | 74 68 65 20 49 50 43 0a |dest of |the IPC.|
|000004c0| 74 6f 6f 6c 73 2c 20 68 | 61 76 69 6e 67 20 62 65 |tools, h|aving be|
|000004d0| 65 6e 20 61 72 6f 75 6e | 64 20 73 69 6e 63 65 20 |en aroun|d since |
|000004e0| 74 68 65 20 65 61 72 6c | 69 65 73 74 20 69 6e 63 |the earl|iest inc|
|000004f0| 61 72 6e 61 74 69 6f 6e | 73 20 6f 66 20 74 68 65 |arnation|s of the|
|00000500| 20 55 4e 49 58 0a 6f 70 | 65 72 61 74 69 6e 67 20 | UNIX.op|erating |
|00000510| 73 79 73 74 65 6d 2e 20 | 20 54 68 65 79 20 70 72 |system. | They pr|
|00000520| 6f 76 69 64 65 20 61 20 | 6d 65 74 68 6f 64 20 6f |ovide a |method o|
|00000530| 66 20 6f 6e 65 2d 77 61 | 79 20 63 6f 6d 6d 75 6e |f one-wa|y commun|
|00000540| 69 63 61 74 69 6f 6e 73 | 20 28 68 65 6e 63 65 0a |ications| (hence.|
|00000550| 74 68 65 20 74 65 72 6d | 20 68 61 6c 66 2d 64 75 |the term| half-du|
|00000560| 70 6c 65 78 29 20 62 65 | 74 77 65 65 6e 20 70 72 |plex) be|tween pr|
|00000570| 6f 63 65 73 73 65 73 2e | 0a 0a 54 68 69 73 20 66 |ocesses.|..This f|
|00000580| 65 61 74 75 72 65 20 69 | 73 20 77 69 64 65 6c 79 |eature i|s widely|
|00000590| 20 75 73 65 64 2c 20 65 | 76 65 6e 20 6f 6e 20 74 | used, e|ven on t|
|000005a0| 68 65 20 55 4e 49 58 20 | 63 6f 6d 6d 61 6e 64 20 |he UNIX |command |
|000005b0| 6c 69 6e 65 20 28 69 6e | 20 74 68 65 20 73 68 65 |line (in| the she|
|000005c0| 6c 6c 29 2e 0a 0a 5c 62 | 65 67 69 6e 7b 74 73 63 |ll)...\b|egin{tsc|
|000005d0| 72 65 65 6e 7d 0a 5c 62 | 65 67 69 6e 7b 76 65 72 |reen}.\b|egin{ver|
|000005e0| 62 61 74 69 6d 7d 0a 20 | 20 20 20 20 20 20 20 6c |batim}. | l|
|000005f0| 73 20 7c 20 73 6f 72 74 | 20 7c 20 6c 70 0a 5c 65 |s | sort| | lp.\e|
|00000600| 6e 64 7b 76 65 72 62 61 | 74 69 6d 7d 0a 0a 0a 5c |nd{verba|tim}...\|
|00000610| 65 6e 64 7b 74 73 63 72 | 65 65 6e 7d 0a 0a 0a 54 |end{tscr|een}...T|
|00000620| 68 65 20 61 62 6f 76 65 | 20 73 65 74 73 20 75 70 |he above| sets up|
|00000630| 20 61 20 70 69 70 65 6c | 69 6e 65 2c 20 74 61 6b | a pipel|ine, tak|
|00000640| 69 6e 67 20 74 68 65 20 | 6f 75 74 70 75 74 20 6f |ing the |output o|
|00000650| 66 20 6c 73 20 61 73 20 | 74 68 65 20 69 6e 70 75 |f ls as |the inpu|
|00000660| 74 20 6f 66 20 0a 73 6f | 72 74 2c 20 61 6e 64 20 |t of .so|rt, and |
|00000670| 74 68 65 20 6f 75 74 70 | 75 74 20 6f 66 20 73 6f |the outp|ut of so|
|00000680| 72 74 20 61 73 20 74 68 | 65 20 69 6e 70 75 74 20 |rt as th|e input |
|00000690| 6f 66 20 6c 70 2e 20 20 | 54 68 65 20 64 61 74 61 |of lp. |The data|
|000006a0| 20 69 73 20 72 75 6e 6e | 69 6e 67 20 74 68 72 6f | is runn|ing thro|
|000006b0| 75 67 68 20 61 0a 68 61 | 6c 66 20 64 75 70 6c 65 |ugh a.ha|lf duple|
|000006c0| 78 20 70 69 70 65 2c 20 | 74 72 61 76 65 6c 69 6e |x pipe, |travelin|
|000006d0| 67 20 28 76 69 73 75 61 | 6c 6c 79 29 20 6c 65 66 |g (visua|lly) lef|
|000006e0| 74 20 74 6f 20 72 69 67 | 68 74 20 74 68 72 6f 75 |t to rig|ht throu|
|000006f0| 67 68 20 74 68 65 20 70 | 69 70 65 6c 69 6e 65 2e |gh the p|ipeline.|
|00000700| 0a 0a 41 6c 74 68 6f 75 | 67 68 20 6d 6f 73 74 20 |..Althou|gh most |
|00000710| 6f 66 20 75 73 20 75 73 | 65 20 70 69 70 65 73 20 |of us us|e pipes |
|00000720| 71 75 69 74 65 20 72 65 | 6c 69 67 69 6f 75 73 6c |quite re|ligiousl|
|00000730| 79 20 69 6e 20 73 68 65 | 6c 6c 20 73 63 72 69 70 |y in she|ll scrip|
|00000740| 74 20 70 72 6f 67 72 61 | 6d 6d 69 6e 67 2c 20 0a |t progra|mming, .|
|00000750| 77 65 20 6f 66 74 65 6e | 20 64 6f 20 73 6f 20 77 |we often| do so w|
|00000760| 69 74 68 6f 75 74 20 67 | 69 76 69 6e 67 20 61 20 |ithout g|iving a |
|00000770| 73 65 63 6f 6e 64 20 74 | 68 6f 75 67 68 74 20 74 |second t|hought t|
|00000780| 6f 20 77 68 61 74 20 74 | 72 61 6e 73 70 69 72 65 |o what t|ranspire|
|00000790| 73 20 61 74 20 74 68 65 | 20 6b 65 72 6e 65 6c 0a |s at the| kernel.|
|000007a0| 6c 65 76 65 6c 2e 20 0a | 0a 57 68 65 6e 20 61 20 |level. .|.When a |
|000007b0| 70 72 6f 63 65 73 73 20 | 63 72 65 61 74 65 73 20 |process |creates |
|000007c0| 61 20 70 69 70 65 2c 20 | 74 68 65 20 6b 65 72 6e |a pipe, |the kern|
|000007d0| 65 6c 20 73 65 74 73 20 | 75 70 20 74 77 6f 20 66 |el sets |up two f|
|000007e0| 69 6c 65 20 64 65 73 63 | 72 69 70 74 6f 72 73 20 |ile desc|riptors |
|000007f0| 66 6f 72 20 75 73 65 0a | 62 79 20 74 68 65 20 70 |for use.|by the p|
|00000800| 69 70 65 2e 20 20 4f 6e | 65 20 64 65 73 63 72 69 |ipe. On|e descri|
|00000810| 70 74 6f 72 20 69 73 20 | 75 73 65 64 20 74 6f 20 |ptor is |used to |
|00000820| 61 6c 6c 6f 77 20 61 20 | 70 61 74 68 20 6f 66 20 |allow a |path of |
|00000830| 69 6e 70 75 74 20 69 6e | 74 6f 20 74 68 65 20 70 |input in|to the p|
|00000840| 69 70 65 20 28 77 72 69 | 74 65 29 2c 0a 77 68 69 |ipe (wri|te),.whi|
|00000850| 6c 65 20 74 68 65 20 6f | 74 68 65 72 20 69 73 20 |le the o|ther is |
|00000860| 75 73 65 64 20 74 6f 20 | 6f 62 74 61 69 6e 20 64 |used to |obtain d|
|00000870| 61 74 61 20 66 72 6f 6d | 20 74 68 65 20 70 69 70 |ata from| the pip|
|00000880| 65 20 28 72 65 61 64 29 | 2e 20 20 41 74 20 74 68 |e (read)|. At th|
|00000890| 69 73 20 70 6f 69 6e 74 | 2c 20 74 68 65 0a 70 69 |is point|, the.pi|
|000008a0| 70 65 20 69 73 20 6f 66 | 20 6c 69 74 74 6c 65 20 |pe is of| little |
|000008b0| 70 72 61 63 74 69 63 61 | 6c 20 75 73 65 2c 20 61 |practica|l use, a|
|000008c0| 73 20 74 68 65 20 63 72 | 65 61 74 69 6e 67 20 70 |s the cr|eating p|
|000008d0| 72 6f 63 65 73 73 20 63 | 61 6e 20 6f 6e 6c 79 20 |rocess c|an only |
|000008e0| 75 73 65 20 74 68 65 20 | 70 69 70 65 20 74 6f 0a |use the |pipe to.|
|000008f0| 63 6f 6d 6d 75 6e 69 63 | 61 74 65 20 77 69 74 68 |communic|ate with|
|00000900| 20 69 74 73 65 6c 66 2e | 20 20 43 6f 6e 73 69 64 | itself.| Consid|
|00000910| 65 72 20 74 68 69 73 20 | 72 65 70 72 65 73 65 6e |er this |represen|
|00000920| 74 61 74 69 6f 6e 20 6f | 66 20 61 20 70 72 6f 63 |tation o|f a proc|
|00000930| 65 73 73 20 61 6e 64 20 | 74 68 65 0a 6b 65 72 6e |ess and |the.kern|
|00000940| 65 6c 20 61 66 74 65 72 | 20 61 20 70 69 70 65 20 |el after| a pipe |
|00000950| 68 61 73 20 62 65 65 6e | 20 63 72 65 61 74 65 64 |has been| created|
|00000960| 3a 0a 0a 5c 62 65 67 69 | 6e 20 7b 63 65 6e 74 65 |:..\begi|n {cente|
|00000970| 72 7d 0a 5c 69 6e 70 75 | 74 20 7b 70 69 70 65 31 |r}.\inpu|t {pipe1|
|00000980| 7d 0a 5c 65 6e 64 20 20 | 20 7b 63 65 6e 74 65 72 |}.\end | {center|
|00000990| 7d 0a 0a 46 72 6f 6d 20 | 74 68 65 20 61 62 6f 76 |}..From |the abov|
|000009a0| 65 20 64 69 61 67 72 61 | 6d 2c 20 69 74 20 69 73 |e diagra|m, it is|
|000009b0| 20 65 61 73 79 20 74 6f | 20 73 65 65 20 68 6f 77 | easy to| see how|
|000009c0| 20 74 68 65 20 64 65 73 | 63 72 69 70 74 6f 72 73 | the des|criptors|
|000009d0| 20 61 72 65 20 63 6f 6e | 6e 65 63 74 65 64 0a 74 | are con|nected.t|
|000009e0| 6f 67 65 74 68 65 72 2e | 20 20 49 66 20 74 68 65 |ogether.| If the|
|000009f0| 20 70 72 6f 63 65 73 73 | 20 73 65 6e 64 73 20 64 | process| sends d|
|00000a00| 61 74 61 20 74 68 72 6f | 75 67 68 20 74 68 65 20 |ata thro|ugh the |
|00000a10| 70 69 70 65 20 28 66 64 | 30 29 2c 20 69 74 20 68 |pipe (fd|0), it h|
|00000a20| 61 73 20 74 68 65 20 61 | 62 69 6c 69 74 79 0a 74 |as the a|bility.t|
|00000a30| 6f 20 6f 62 74 61 69 6e | 20 28 72 65 61 64 29 20 |o obtain| (read) |
|00000a40| 74 68 61 74 20 69 6e 66 | 6f 72 6d 61 74 69 6f 6e |that inf|ormation|
|00000a50| 20 66 72 6f 6d 20 66 64 | 31 2e 20 20 48 6f 77 65 | from fd|1. Howe|
|00000a60| 76 65 72 2c 20 74 68 65 | 72 65 20 69 73 20 61 20 |ver, the|re is a |
|00000a70| 6d 75 63 68 20 6c 61 72 | 67 65 72 0a 6f 62 6a 65 |much lar|ger.obje|
|00000a80| 63 74 69 76 65 20 6f 66 | 20 74 68 65 20 73 69 6d |ctive of| the sim|
|00000a90| 70 6c 69 73 74 69 63 20 | 73 6b 65 74 63 68 20 61 |plistic |sketch a|
|00000aa0| 62 6f 76 65 2e 20 20 57 | 68 69 6c 65 20 61 20 70 |bove. W|hile a p|
|00000ab0| 69 70 65 20 69 6e 69 74 | 69 61 6c 6c 79 20 63 6f |ipe init|ially co|
|00000ac0| 6e 6e 65 63 74 73 20 61 | 20 0a 70 72 6f 63 65 73 |nnects a| .proces|
|00000ad0| 73 20 74 6f 20 69 74 73 | 65 6c 66 2c 20 64 61 74 |s to its|elf, dat|
|00000ae0| 61 20 74 72 61 76 65 6c | 69 6e 67 20 74 68 72 6f |a travel|ing thro|
|00000af0| 75 67 68 20 74 68 65 20 | 70 69 70 65 20 6d 6f 76 |ugh the |pipe mov|
|00000b00| 65 73 20 74 68 72 6f 75 | 67 68 20 74 68 65 20 6b |es throu|gh the k|
|00000b10| 65 72 6e 65 6c 2e 0a 55 | 6e 64 65 72 20 4c 69 6e |ernel..U|nder Lin|
|00000b20| 75 78 2c 20 69 6e 20 70 | 61 72 74 69 63 75 6c 61 |ux, in p|articula|
|00000b30| 72 2c 20 70 69 70 65 73 | 20 61 72 65 20 61 63 74 |r, pipes| are act|
|00000b40| 75 61 6c 6c 79 20 72 65 | 70 72 65 73 65 6e 74 65 |ually re|presente|
|00000b50| 64 20 69 6e 74 65 72 6e | 61 6c 6c 79 20 77 69 74 |d intern|ally wit|
|00000b60| 68 20 61 0a 76 61 6c 69 | 64 20 69 6e 6f 64 65 2e |h a.vali|d inode.|
|00000b70| 20 20 4f 66 20 63 6f 75 | 72 73 65 2c 20 74 68 69 | Of cou|rse, thi|
|00000b80| 73 20 69 6e 6f 64 65 20 | 72 65 73 69 64 65 73 20 |s inode |resides |
|00000b90| 77 69 74 68 69 6e 20 74 | 68 65 20 6b 65 72 6e 65 |within t|he kerne|
|00000ba0| 6c 20 69 74 73 65 6c 66 | 2c 20 61 6e 64 20 6e 6f |l itself|, and no|
|00000bb0| 74 0a 77 69 74 68 69 6e | 20 74 68 65 20 62 6f 75 |t.within| the bou|
|00000bc0| 6e 64 73 20 6f 66 20 61 | 6e 79 20 70 68 79 73 69 |nds of a|ny physi|
|00000bd0| 63 61 6c 20 66 69 6c 65 | 20 73 79 73 74 65 6d 2e |cal file| system.|
|00000be0| 20 20 54 68 69 73 20 70 | 61 72 74 69 63 75 6c 61 | This p|articula|
|00000bf0| 72 20 70 6f 69 6e 74 20 | 77 69 6c 6c 0a 6f 70 65 |r point |will.ope|
|00000c00| 6e 20 75 70 20 73 6f 6d | 65 20 70 72 65 74 74 79 |n up som|e pretty|
|00000c10| 20 68 61 6e 64 79 20 49 | 2f 4f 20 64 6f 6f 72 73 | handy I|/O doors|
|00000c20| 20 66 6f 72 20 75 73 2c | 20 61 73 20 77 65 20 77 | for us,| as we w|
|00000c30| 69 6c 6c 20 73 65 65 20 | 61 20 62 69 74 20 6c 61 |ill see |a bit la|
|00000c40| 74 65 72 20 6f 6e 2e 0a | 0a 41 74 20 74 68 69 73 |ter on..|.At this|
|00000c50| 20 70 6f 69 6e 74 2c 20 | 74 68 65 20 70 69 70 65 | point, |the pipe|
|00000c60| 20 69 73 20 66 61 69 72 | 6c 79 20 75 73 65 6c 65 | is fair|ly usele|
|00000c70| 73 73 2e 20 20 41 66 74 | 65 72 20 61 6c 6c 2c 20 |ss. Aft|er all, |
|00000c80| 77 68 79 20 67 6f 20 74 | 6f 20 74 68 65 20 74 72 |why go t|o the tr|
|00000c90| 6f 75 62 6c 65 20 6f 66 | 0a 63 72 65 61 74 69 6e |ouble of|.creatin|
|00000ca0| 67 20 61 20 70 69 70 65 | 20 69 66 20 77 65 20 61 |g a pipe| if we a|
|00000cb0| 72 65 20 6f 6e 6c 79 20 | 67 6f 69 6e 67 20 74 6f |re only |going to|
|00000cc0| 20 74 61 6c 6b 20 74 6f | 20 6f 75 72 73 65 6c 66 | talk to| ourself|
|00000cd0| 3f 20 20 41 74 20 74 68 | 69 73 20 70 6f 69 6e 74 |? At th|is point|
|00000ce0| 2c 20 74 68 65 20 0a 63 | 72 65 61 74 69 6e 67 20 |, the .c|reating |
|00000cf0| 70 72 6f 63 65 73 73 20 | 74 79 70 69 63 61 6c 6c |process |typicall|
|00000d00| 79 20 66 6f 72 6b 73 20 | 61 20 63 68 69 6c 64 20 |y forks |a child |
|00000d10| 70 72 6f 63 65 73 73 2e | 20 20 53 69 6e 63 65 20 |process.| Since |
|00000d20| 61 20 63 68 69 6c 64 20 | 70 72 6f 63 65 73 73 20 |a child |process |
|00000d30| 77 69 6c 6c 0a 69 6e 68 | 65 72 69 74 20 61 6e 79 |will.inh|erit any|
|00000d40| 20 6f 70 65 6e 20 66 69 | 6c 65 20 64 65 73 63 72 | open fi|le descr|
|00000d50| 69 70 74 6f 72 73 20 66 | 72 6f 6d 20 74 68 65 20 |iptors f|rom the |
|00000d60| 70 61 72 65 6e 74 2c 20 | 77 65 20 6e 6f 77 20 68 |parent, |we now h|
|00000d70| 61 76 65 20 74 68 65 20 | 62 61 73 69 73 20 66 6f |ave the |basis fo|
|00000d80| 72 0a 6d 75 6c 74 69 70 | 72 6f 63 65 73 73 20 63 |r.multip|rocess c|
|00000d90| 6f 6d 6d 75 6e 69 63 61 | 74 69 6f 6e 20 28 62 65 |ommunica|tion (be|
|00000da0| 74 77 65 65 6e 20 70 61 | 72 65 6e 74 20 61 6e 64 |tween pa|rent and|
|00000db0| 20 63 68 69 6c 64 29 2e | 20 20 43 6f 6e 73 69 64 | child).| Consid|
|00000dc0| 65 72 20 74 68 69 73 20 | 75 70 64 61 74 65 64 0a |er this |updated.|
|00000dd0| 76 65 72 73 69 6f 6e 20 | 6f 66 20 6f 75 72 20 73 |version |of our s|
|00000de0| 69 6d 70 6c 65 20 73 6b | 65 74 63 68 3a 0a 0a 5c |imple sk|etch:..\|
|00000df0| 62 65 67 69 6e 20 7b 63 | 65 6e 74 65 72 7d 0a 5c |begin {c|enter}.\|
|00000e00| 69 6e 70 75 74 20 7b 70 | 69 70 65 32 7d 0a 5c 65 |input {p|ipe2}.\e|
|00000e10| 6e 64 20 20 20 7b 63 65 | 6e 74 65 72 7d 0a 0a 41 |nd {ce|nter}..A|
|00000e20| 62 6f 76 65 2c 20 77 65 | 20 73 65 65 20 74 68 61 |bove, we| see tha|
|00000e30| 74 20 62 6f 74 68 20 70 | 72 6f 63 65 73 73 65 73 |t both p|rocesses|
|00000e40| 20 6e 6f 77 20 68 61 76 | 65 20 61 63 63 65 73 73 | now hav|e access|
|00000e50| 20 74 6f 20 74 68 65 20 | 66 69 6c 65 20 64 65 73 | to the |file des|
|00000e60| 63 72 69 70 74 6f 72 73 | 20 77 68 69 63 68 0a 63 |criptors| which.c|
|00000e70| 6f 6e 73 74 69 74 75 74 | 65 20 74 68 65 20 70 69 |onstitut|e the pi|
|00000e80| 70 65 6c 69 6e 65 2e 20 | 20 49 74 20 69 73 20 61 |peline. | It is a|
|00000e90| 74 20 74 68 69 73 20 73 | 74 61 67 65 2c 20 74 68 |t this s|tage, th|
|00000ea0| 61 74 20 61 20 63 72 69 | 74 69 63 61 6c 20 64 65 |at a cri|tical de|
|00000eb0| 63 69 73 69 6f 6e 20 6d | 75 73 74 20 62 65 0a 6d |cision m|ust be.m|
|00000ec0| 61 64 65 2e 20 20 49 6e | 20 77 68 69 63 68 20 64 |ade. In| which d|
|00000ed0| 69 72 65 63 74 69 6f 6e | 20 64 6f 20 77 65 20 64 |irection| do we d|
|00000ee0| 65 73 69 72 65 20 64 61 | 74 61 20 74 6f 20 74 72 |esire da|ta to tr|
|00000ef0| 61 76 65 6c 3f 20 20 44 | 6f 65 73 20 74 68 65 20 |avel? D|oes the |
|00000f00| 63 68 69 6c 64 20 70 72 | 6f 63 65 73 73 0a 73 65 |child pr|ocess.se|
|00000f10| 6e 64 20 69 6e 66 6f 72 | 6d 61 74 69 6f 6e 20 74 |nd infor|mation t|
|00000f20| 6f 20 74 68 65 20 70 61 | 72 65 6e 74 2c 20 6f 72 |o the pa|rent, or|
|00000f30| 20 76 69 63 65 2d 76 65 | 72 73 61 3f 20 20 54 68 | vice-ve|rsa? Th|
|00000f40| 65 20 74 77 6f 20 70 72 | 6f 63 65 73 73 65 73 20 |e two pr|ocesses |
|00000f50| 6d 75 74 75 61 6c 6c 79 | 20 61 67 72 65 65 0a 6f |mutually| agree.o|
|00000f60| 6e 20 74 68 69 73 20 69 | 73 73 75 65 2c 20 61 6e |n this i|ssue, an|
|00000f70| 64 20 70 72 6f 63 65 65 | 64 20 74 6f 20 60 60 63 |d procee|d to ``c|
|00000f80| 6c 6f 73 65 27 27 20 74 | 68 65 20 65 6e 64 20 6f |lose'' t|he end o|
|00000f90| 66 20 74 68 65 20 70 69 | 70 65 20 74 68 61 74 20 |f the pi|pe that |
|00000fa0| 74 68 65 79 20 61 72 65 | 20 6e 6f 74 0a 63 6f 6e |they are| not.con|
|00000fb0| 63 65 72 6e 65 64 20 77 | 69 74 68 2e 20 20 46 6f |cerned w|ith. Fo|
|00000fc0| 72 20 64 69 73 63 75 73 | 73 69 6f 6e 20 70 75 72 |r discus|sion pur|
|00000fd0| 70 6f 73 65 73 2c 20 6c | 65 74 27 73 20 73 61 79 |poses, l|et's say|
|00000fe0| 20 74 68 65 20 63 68 69 | 6c 64 20 70 65 72 66 6f | the chi|ld perfo|
|00000ff0| 72 6d 73 20 73 6f 6d 65 | 0a 70 72 6f 63 65 73 73 |rms some|.process|
|00001000| 69 6e 67 2c 20 61 6e 64 | 20 73 65 6e 64 73 20 69 |ing, and| sends i|
|00001010| 6e 66 6f 72 6d 61 74 69 | 6f 6e 20 62 61 63 6b 20 |nformati|on back |
|00001020| 74 68 72 6f 75 67 68 20 | 74 68 65 20 70 69 70 65 |through |the pipe|
|00001030| 20 74 6f 20 74 68 65 20 | 70 61 72 65 6e 74 2e 20 | to the |parent. |
|00001040| 20 4f 75 72 0a 6e 65 77 | 6c 79 20 72 65 76 69 73 | Our.new|ly revis|
|00001050| 65 64 20 73 6b 65 74 63 | 68 20 77 6f 75 6c 64 20 |ed sketc|h would |
|00001060| 61 70 70 65 61 72 20 61 | 73 20 73 75 63 68 3a 0a |appear a|s such:.|
|00001070| 0a 5c 62 65 67 69 6e 20 | 7b 63 65 6e 74 65 72 7d |.\begin |{center}|
|00001080| 0a 5c 69 6e 70 75 74 20 | 7b 70 69 70 65 33 7d 0a |.\input |{pipe3}.|
|00001090| 5c 65 6e 64 20 20 20 7b | 63 65 6e 74 65 72 7d 0a |\end {|center}.|
|000010a0| 0a 43 6f 6e 73 74 72 75 | 63 74 69 6f 6e 20 6f 66 |.Constru|ction of|
|000010b0| 20 74 68 65 20 70 69 70 | 65 6c 69 6e 65 20 69 73 | the pip|eline is|
|000010c0| 20 6e 6f 77 20 63 6f 6d | 70 6c 65 74 65 21 20 20 | now com|plete! |
|000010d0| 54 68 65 20 6f 6e 6c 79 | 20 74 68 69 6e 67 20 6c |The only| thing l|
|000010e0| 65 66 74 20 74 6f 20 64 | 6f 20 69 73 0a 6d 61 6b |eft to d|o is.mak|
|000010f0| 65 20 75 73 65 20 6f 66 | 20 74 68 65 20 70 69 70 |e use of| the pip|
|00001100| 65 2e 20 20 54 6f 20 61 | 63 63 65 73 73 20 61 20 |e. To a|ccess a |
|00001110| 70 69 70 65 20 64 69 72 | 65 63 74 6c 79 2c 20 74 |pipe dir|ectly, t|
|00001120| 68 65 20 73 61 6d 65 20 | 73 79 73 74 65 6d 20 63 |he same |system c|
|00001130| 61 6c 6c 73 20 74 68 61 | 74 0a 61 72 65 20 75 73 |alls tha|t.are us|
|00001140| 65 64 20 66 6f 72 20 6c | 6f 77 2d 6c 65 76 65 6c |ed for l|ow-level|
|00001150| 20 66 69 6c 65 20 49 2f | 4f 20 63 61 6e 20 62 65 | file I/|O can be|
|00001160| 20 75 73 65 64 20 28 72 | 65 63 61 6c 6c 20 74 68 | used (r|ecall th|
|00001170| 61 74 20 70 69 70 65 73 | 20 61 72 65 20 61 63 74 |at pipes| are act|
|00001180| 75 61 6c 6c 79 0a 72 65 | 70 72 65 73 65 6e 74 65 |ually.re|presente|
|00001190| 64 20 69 6e 74 65 72 6e | 61 6c 6c 79 20 61 73 20 |d intern|ally as |
|000011a0| 61 20 76 61 6c 69 64 20 | 69 6e 6f 64 65 29 2e 0a |a valid |inode)..|
|000011b0| 0a 54 6f 20 73 65 6e 64 | 20 64 61 74 61 20 74 6f |.To send| data to|
|000011c0| 20 74 68 65 20 70 69 70 | 65 2c 20 77 65 20 75 73 | the pip|e, we us|
|000011d0| 65 20 74 68 65 20 77 72 | 69 74 65 28 29 20 73 79 |e the wr|ite() sy|
|000011e0| 73 74 65 6d 20 63 61 6c | 6c 2c 20 61 6e 64 20 74 |stem cal|l, and t|
|000011f0| 6f 20 72 65 74 72 69 65 | 76 65 20 64 61 74 61 0a |o retrie|ve data.|
|00001200| 66 72 6f 6d 20 74 68 65 | 20 70 69 70 65 2c 20 77 |from the| pipe, w|
|00001210| 65 20 75 73 65 20 74 68 | 65 20 72 65 61 64 28 29 |e use th|e read()|
|00001220| 20 73 79 73 74 65 6d 20 | 63 61 6c 6c 2e 20 20 52 | system |call. R|
|00001230| 65 6d 65 6d 62 65 72 2c | 20 6c 6f 77 2d 6c 65 76 |emember,| low-lev|
|00001240| 65 6c 20 66 69 6c 65 20 | 49 2f 4f 0a 73 79 73 74 |el file |I/O.syst|
|00001250| 65 6d 20 63 61 6c 6c 73 | 20 77 6f 72 6b 20 77 69 |em calls| work wi|
|00001260| 74 68 20 66 69 6c 65 20 | 64 65 73 63 72 69 70 74 |th file |descript|
|00001270| 6f 72 73 21 20 20 48 6f | 77 65 76 65 72 2c 20 6b |ors! Ho|wever, k|
|00001280| 65 65 70 20 69 6e 20 6d | 69 6e 64 20 74 68 61 74 |eep in m|ind that|
|00001290| 20 63 65 72 74 61 69 6e | 0a 73 79 73 74 65 6d 20 | certain|.system |
|000012a0| 63 61 6c 6c 73 2c 20 73 | 75 63 68 20 61 73 20 6c |calls, s|uch as l|
|000012b0| 73 65 65 6b 28 29 2c 20 | 64 6f 20 6e 6f 74 20 77 |seek(), |do not w|
|000012c0| 6f 72 6b 20 77 69 74 68 | 20 64 65 73 63 72 69 70 |ork with| descrip|
|000012d0| 74 6f 72 73 20 74 6f 20 | 70 69 70 65 73 2e 0a 0a |tors to |pipes...|
|000012e0| 0a 0a 0a 5c 73 75 62 73 | 65 63 74 69 6f 6e 7b 43 |...\subs|ection{C|
|000012f0| 72 65 61 74 69 6e 67 20 | 50 69 70 65 73 20 69 6e |reating |Pipes in|
|00001300| 20 43 7d 0a 0a 0a 0a 43 | 72 65 61 74 69 6e 67 20 | C}....C|reating |
|00001310| 60 60 70 69 70 65 6c 69 | 6e 65 73 27 27 20 77 69 |``pipeli|nes'' wi|
|00001320| 74 68 20 74 68 65 20 43 | 20 70 72 6f 67 72 61 6d |th the C| program|
|00001330| 6d 69 6e 67 20 6c 61 6e | 67 75 61 67 65 20 63 61 |ming lan|guage ca|
|00001340| 6e 20 62 65 20 61 20 62 | 69 74 20 6d 6f 72 65 20 |n be a b|it more |
|00001350| 69 6e 76 6f 6c 76 65 64 | 0a 74 68 61 6e 20 6f 75 |involved|.than ou|
|00001360| 72 20 73 69 6d 70 6c 65 | 20 73 68 65 6c 6c 20 65 |r simple| shell e|
|00001370| 78 61 6d 70 6c 65 2e 20 | 20 54 6f 20 63 72 65 61 |xample. | To crea|
|00001380| 74 65 20 61 20 73 69 6d | 70 6c 65 20 70 69 70 65 |te a sim|ple pipe|
|00001390| 20 77 69 74 68 20 43 2c | 20 77 65 20 6d 61 6b 65 | with C,| we make|
|000013a0| 20 75 73 65 20 6f 66 0a | 74 68 65 20 70 69 70 65 | use of.|the pipe|
|000013b0| 28 29 20 73 79 73 74 65 | 6d 20 63 61 6c 6c 2e 20 |() syste|m call. |
|000013c0| 20 49 74 20 74 61 6b 65 | 73 20 61 20 73 69 6e 67 | It take|s a sing|
|000013d0| 6c 65 20 61 72 67 75 6d | 65 6e 74 2c 20 77 68 69 |le argum|ent, whi|
|000013e0| 63 68 20 69 73 20 61 6e | 20 61 72 72 61 79 20 6f |ch is an| array o|
|000013f0| 66 20 74 77 6f 0a 69 6e | 74 65 67 65 72 73 2c 20 |f two.in|tegers, |
|00001400| 61 6e 64 20 69 66 20 73 | 75 63 63 65 73 73 66 75 |and if s|uccessfu|
|00001410| 6c 2c 20 74 68 65 20 61 | 72 72 61 79 20 77 69 6c |l, the a|rray wil|
|00001420| 6c 20 63 6f 6e 74 61 69 | 6e 20 74 77 6f 20 6e 65 |l contai|n two ne|
|00001430| 77 20 66 69 6c 65 20 64 | 65 73 63 72 69 70 74 6f |w file d|escripto|
|00001440| 72 73 0a 74 6f 20 62 65 | 20 75 73 65 64 20 66 6f |rs.to be| used fo|
|00001450| 72 20 74 68 65 20 70 69 | 70 65 6c 69 6e 65 2e 20 |r the pi|peline. |
|00001460| 41 66 74 65 72 20 63 72 | 65 61 74 69 6e 67 20 61 |After cr|eating a|
|00001470| 20 70 69 70 65 2c 20 74 | 68 65 20 70 72 6f 63 65 | pipe, t|he proce|
|00001480| 73 73 20 74 79 70 69 63 | 61 6c 6c 79 0a 73 70 61 |ss typic|ally.spa|
|00001490| 77 6e 73 20 61 20 6e 65 | 77 20 70 72 6f 63 65 73 |wns a ne|w proces|
|000014a0| 73 20 28 72 65 6d 65 6d | 62 65 72 20 74 68 65 20 |s (remem|ber the |
|000014b0| 63 68 69 6c 64 20 69 6e | 68 65 72 69 74 73 20 6f |child in|herits o|
|000014c0| 70 65 6e 20 66 69 6c 65 | 20 64 65 73 63 72 69 70 |pen file| descrip|
|000014d0| 74 6f 72 73 29 2e 0a 0a | 0a 0a 5c 70 61 72 0a 5c |tors)...|..\par.\|
|000014e0| 61 64 64 76 73 70 61 63 | 65 7b 5c 6d 65 64 73 6b |addvspac|e{\medsk|
|000014f0| 69 70 61 6d 6f 75 6e 74 | 7d 0a 5c 6e 6f 70 61 67 |ipamount|}.\nopag|
|00001500| 65 62 72 65 61 6b 5c 68 | 72 75 6c 65 0a 5c 62 65 |ebreak\h|rule.\be|
|00001510| 67 69 6e 7b 76 65 72 62 | 61 74 69 6d 7d 0a 20 20 |gin{verb|atim}. |
|00001520| 53 59 53 54 45 4d 20 43 | 41 4c 4c 3a 20 70 69 70 |SYSTEM C|ALL: pip|
|00001530| 65 28 29 3b 20 20 20 20 | 20 20 20 20 20 20 20 20 |e(); | |
|00001540| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00001550| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00001560| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 0a 0a | | ..|
|00001570| 20 20 50 52 4f 54 4f 54 | 59 50 45 3a 20 69 6e 74 | PROTOT|YPE: int|
|00001580| 20 70 69 70 65 28 20 69 | 6e 74 20 66 64 5b 32 5d | pipe( i|nt fd[2]|
|00001590| 20 29 3b 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | ); | |
|000015a0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000015b0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000015c0| 0a 20 20 20 20 52 45 54 | 55 52 4e 53 3a 20 30 20 |. RET|URNS: 0 |
|000015d0| 6f 6e 20 73 75 63 63 65 | 73 73 20 20 20 20 20 20 |on succe|ss |
|000015e0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000015f0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00001600| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00001610| 20 0a 20 20 20 20 20 20 | 20 20 20 20 20 20 20 2d | . | -|
|00001620| 31 20 6f 6e 20 65 72 72 | 6f 72 3a 20 65 72 72 6e |1 on err|or: errn|
|00001630| 6f 20 3d 20 45 4d 46 49 | 4c 45 20 28 6e 6f 20 66 |o = EMFI|LE (no f|
|00001640| 72 65 65 20 64 65 73 63 | 72 69 70 74 6f 72 73 29 |ree desc|riptors)|
|00001650| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00001660| 20 20 0a 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | . | |
|00001670| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00001680| 20 20 20 20 20 45 4d 46 | 49 4c 45 20 28 73 79 73 | EMF|ILE (sys|
|00001690| 74 65 6d 20 66 69 6c 65 | 20 74 61 62 6c 65 20 69 |tem file| table i|
|000016a0| 73 20 66 75 6c 6c 29 20 | 20 20 20 20 20 20 20 20 |s full) | |
|000016b0| 20 20 20 0a 20 20 20 20 | 20 20 20 20 20 20 20 20 | . | |
|000016c0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000016d0| 20 20 20 20 20 20 45 46 | 41 55 4c 54 20 28 66 64 | EF|AULT (fd|
|000016e0| 20 61 72 72 61 79 20 69 | 73 20 6e 6f 74 20 76 61 | array i|s not va|
|000016f0| 6c 69 64 29 20 20 20 20 | 20 20 20 20 20 20 20 20 |lid) | |
|00001700| 20 20 20 20 0a 0a 20 20 | 4e 4f 54 45 53 3a 20 66 | .. |NOTES: f|
|00001710| 64 5b 30 5d 20 69 73 20 | 73 65 74 20 75 70 20 66 |d[0] is |set up f|
|00001720| 6f 72 20 72 65 61 64 69 | 6e 67 2c 20 66 64 5b 31 |or readi|ng, fd[1|
|00001730| 5d 20 69 73 20 73 65 74 | 20 75 70 20 66 6f 72 20 |] is set| up for |
|00001740| 77 72 69 74 69 6e 67 20 | 20 20 20 20 20 20 20 20 |writing | |
|00001750| 20 20 20 20 20 20 0a 5c | 65 6e 64 7b 76 65 72 62 | .\|end{verb|
|00001760| 61 74 69 6d 7d 20 0a 5c | 6e 6f 70 61 67 65 62 72 |atim} .\|nopagebr|
|00001770| 65 61 6b 5c 68 72 75 6c | 65 20 0a 5c 61 64 64 76 |eak\hrul|e .\addv|
|00001780| 73 70 61 63 65 7b 5c 6d | 65 64 73 6b 69 70 61 6d |space{\m|edskipam|
|00001790| 6f 75 6e 74 7d 0a 0a 0a | 0a 0a 54 68 65 20 66 69 |ount}...|..The fi|
|000017a0| 72 73 74 20 69 6e 74 65 | 67 65 72 20 69 6e 20 74 |rst inte|ger in t|
|000017b0| 68 65 20 61 72 72 61 79 | 20 28 65 6c 65 6d 65 6e |he array| (elemen|
|000017c0| 74 20 30 29 20 69 73 20 | 73 65 74 20 75 70 20 61 |t 0) is |set up a|
|000017d0| 6e 64 20 6f 70 65 6e 65 | 64 20 66 6f 72 20 72 65 |nd opene|d for re|
|000017e0| 61 64 69 6e 67 2c 0a 77 | 68 69 6c 65 20 74 68 65 |ading,.w|hile the|
|000017f0| 20 73 65 63 6f 6e 64 20 | 69 6e 74 65 67 65 72 20 | second |integer |
|00001800| 28 65 6c 65 6d 65 6e 74 | 20 31 29 20 69 73 20 73 |(element| 1) is s|
|00001810| 65 74 20 75 70 20 61 6e | 64 20 6f 70 65 6e 65 64 |et up an|d opened|
|00001820| 20 66 6f 72 20 77 72 69 | 74 69 6e 67 2e 20 20 0a | for wri|ting. .|
|00001830| 56 69 73 75 61 6c 6c 79 | 20 73 70 65 61 6b 69 6e |Visually| speakin|
|00001840| 67 2c 20 74 68 65 20 6f | 75 74 70 75 74 20 6f 66 |g, the o|utput of|
|00001850| 20 66 64 24 31 24 20 62 | 65 63 6f 6d 65 73 20 74 | fd$1$ b|ecomes t|
|00001860| 68 65 20 69 6e 70 75 74 | 20 66 6f 72 20 66 64 24 |he input| for fd$|
|00001870| 30 24 2e 20 20 4f 6e 63 | 65 0a 61 67 61 69 6e 2c |0$. Onc|e.again,|
|00001880| 20 61 6c 6c 20 64 61 74 | 61 20 74 72 61 76 65 6c | all dat|a travel|
|00001890| 69 6e 67 20 74 68 72 6f | 75 67 68 20 74 68 65 20 |ing thro|ugh the |
|000018a0| 70 69 70 65 20 6d 6f 76 | 65 73 20 74 68 72 6f 75 |pipe mov|es throu|
|000018b0| 67 68 20 74 68 65 20 6b | 65 72 6e 65 6c 2e 0a 0a |gh the k|ernel...|
|000018c0| 5c 62 65 67 69 6e 7b 76 | 65 72 62 61 74 69 6d 7d |\begin{v|erbatim}|
|000018d0| 0a 20 20 20 20 20 20 20 | 20 23 69 6e 63 6c 75 64 |. | #includ|
|000018e0| 65 20 3c 73 74 64 69 6f | 2e 68 3e 0a 20 20 20 20 |e <stdio|.h>. |
|000018f0| 20 20 20 20 23 69 6e 63 | 6c 75 64 65 20 3c 75 6e | #inc|lude <un|
|00001900| 69 73 74 64 2e 68 3e 0a | 20 20 20 20 20 20 20 20 |istd.h>.| |
|00001910| 23 69 6e 63 6c 75 64 65 | 20 3c 73 79 73 2f 74 79 |#include| <sys/ty|
|00001920| 70 65 73 2e 68 3e 0a 0a | 20 20 20 20 20 20 20 20 |pes.h>..| |
|00001930| 6d 61 69 6e 28 29 0a 20 | 20 20 20 20 20 20 20 7b |main(). | {|
|00001940| 0a 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |. | |
|00001950| 20 69 6e 74 20 20 20 20 | 20 66 64 5b 32 5d 3b 0a | int | fd[2];.|
|00001960| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00001970| 0a 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |. | |
|00001980| 20 70 69 70 65 28 66 64 | 29 3b 0a 20 20 20 20 20 | pipe(fd|);. |
|00001990| 20 20 20 20 20 20 20 20 | 20 20 20 2e 0a 20 20 20 | | .. |
|000019a0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 2e 0a 20 | | .. |
|000019b0| 20 20 20 20 20 20 20 7d | 0a 5c 65 6e 64 7b 76 65 | }|.\end{ve|
|000019c0| 72 62 61 74 69 6d 7d 0a | 0a 0a 0a 0a 52 65 6d 65 |rbatim}.|....Reme|
|000019d0| 6d 62 65 72 20 74 68 61 | 74 20 61 6e 20 61 72 72 |mber tha|t an arr|
|000019e0| 61 79 20 6e 61 6d 65 20 | 69 6e 20 43 20 7b 5c 65 |ay name |in C {\e|
|000019f0| 6d 20 64 65 63 61 79 73 | 5c 2f 7d 20 69 6e 74 6f |m decays|\/} into|
|00001a00| 20 61 20 70 6f 69 6e 74 | 65 72 20 74 6f 20 69 74 | a point|er to it|
|00001a10| 73 20 66 69 72 73 74 0a | 6d 65 6d 62 65 72 2e 20 |s first.|member. |
|00001a20| 20 41 62 6f 76 65 2c 20 | 7b 5c 74 74 20 66 64 7d | Above, |{\tt fd}|
|00001a30| 20 69 73 20 65 71 75 69 | 76 61 6c 65 6e 74 20 74 | is equi|valent t|
|00001a40| 6f 20 7b 5c 74 74 20 5c | 26 66 64 5b 30 5d 7d 2e |o {\tt \|&fd[0]}.|
|00001a50| 20 20 4f 6e 63 65 20 77 | 65 20 68 61 76 65 20 65 | Once w|e have e|
|00001a60| 73 74 61 62 6c 69 73 68 | 65 64 20 74 68 65 0a 70 |stablish|ed the.p|
|00001a70| 69 70 65 6c 69 6e 65 2c | 20 77 65 20 74 68 65 6e |ipeline,| we then|
|00001a80| 20 66 6f 72 6b 20 6f 75 | 72 20 6e 65 77 20 63 68 | fork ou|r new ch|
|00001a90| 69 6c 64 20 70 72 6f 63 | 65 73 73 3a 0a 0a 5c 62 |ild proc|ess:..\b|
|00001aa0| 65 67 69 6e 7b 76 65 72 | 62 61 74 69 6d 7d 0a 20 |egin{ver|batim}. |
|00001ab0| 20 20 20 20 20 20 20 23 | 69 6e 63 6c 75 64 65 20 | #|include |
|00001ac0| 3c 73 74 64 69 6f 2e 68 | 3e 0a 20 20 20 20 20 20 |<stdio.h|>. |
|00001ad0| 20 20 23 69 6e 63 6c 75 | 64 65 20 3c 75 6e 69 73 | #inclu|de <unis|
|00001ae0| 74 64 2e 68 3e 0a 20 20 | 20 20 20 20 20 20 23 69 |td.h>. | #i|
|00001af0| 6e 63 6c 75 64 65 20 3c | 73 79 73 2f 74 79 70 65 |nclude <|sys/type|
|00001b00| 73 2e 68 3e 0a 0a 20 20 | 20 20 20 20 20 20 6d 61 |s.h>.. | ma|
|00001b10| 69 6e 28 29 0a 20 20 20 | 20 20 20 20 20 7b 0a 20 |in(). | {. |
|00001b20| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 69 | | i|
|00001b30| 6e 74 20 20 20 20 20 66 | 64 5b 32 5d 3b 0a 20 20 |nt f|d[2];. |
|00001b40| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 70 69 | | pi|
|00001b50| 64 5f 74 20 20 20 63 68 | 69 6c 64 70 69 64 3b 0a |d_t ch|ildpid;.|
|00001b60| 0a 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |. | |
|00001b70| 20 70 69 70 65 28 66 64 | 29 3b 0a 20 20 20 20 20 | pipe(fd|);. |
|00001b80| 20 20 20 0a 20 20 20 20 | 20 20 20 20 20 20 20 20 | . | |
|00001b90| 20 20 20 20 69 66 28 28 | 63 68 69 6c 64 70 69 64 | if((|childpid|
|00001ba0| 20 3d 20 66 6f 72 6b 28 | 29 29 20 3d 3d 20 2d 31 | = fork(|)) == -1|
|00001bb0| 29 0a 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |). | |
|00001bc0| 20 20 7b 0a 20 20 20 20 | 20 20 20 20 20 20 20 20 | {. | |
|00001bd0| 20 20 20 20 20 20 20 20 | 20 20 20 20 70 65 72 72 | | perr|
|00001be0| 6f 72 28 22 66 6f 72 6b | 22 29 3b 0a 20 20 20 20 |or("fork|");. |
|00001bf0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00001c00| 20 20 20 20 65 78 69 74 | 28 31 29 3b 0a 20 20 20 | exit|(1);. |
|00001c10| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 7d 0a 20 | | }. |
|00001c20| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 2e | | .|
|00001c30| 0a 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |. | |
|00001c40| 20 2e 0a 20 20 20 20 20 | 20 20 20 7d 0a 5c 65 6e | .. | }.\en|
|00001c50| 64 7b 76 65 72 62 61 74 | 69 6d 7d 0a 0a 0a 0a 0a |d{verbat|im}.....|
|00001c60| 49 66 20 74 68 65 20 70 | 61 72 65 6e 74 20 77 61 |If the p|arent wa|
|00001c70| 6e 74 73 20 74 6f 20 72 | 65 63 65 69 76 65 20 64 |nts to r|eceive d|
|00001c80| 61 74 61 20 66 72 6f 6d | 20 74 68 65 20 63 68 69 |ata from| the chi|
|00001c90| 6c 64 2c 20 69 74 20 73 | 68 6f 75 6c 64 20 63 6c |ld, it s|hould cl|
|00001ca0| 6f 73 65 20 66 64 24 31 | 24 2c 0a 61 6e 64 20 74 |ose fd$1|$,.and t|
|00001cb0| 68 65 20 63 68 69 6c 64 | 20 73 68 6f 75 6c 64 20 |he child| should |
|00001cc0| 63 6c 6f 73 65 20 66 64 | 24 30 24 2e 20 49 66 20 |close fd|$0$. If |
|00001cd0| 74 68 65 20 70 61 72 65 | 6e 74 20 77 61 6e 74 73 |the pare|nt wants|
|00001ce0| 20 74 6f 20 73 65 6e 64 | 20 64 61 74 61 20 74 6f | to send| data to|
|00001cf0| 20 74 68 65 0a 63 68 69 | 6c 64 2c 20 69 74 20 73 | the.chi|ld, it s|
|00001d00| 68 6f 75 6c 64 20 63 6c | 6f 73 65 20 66 64 24 30 |hould cl|ose fd$0|
|00001d10| 24 2c 20 61 6e 64 20 74 | 68 65 20 63 68 69 6c 64 |$, and t|he child|
|00001d20| 20 73 68 6f 75 6c 64 20 | 63 6c 6f 73 65 20 66 64 | should |close fd|
|00001d30| 24 31 24 2e 20 53 69 6e | 63 65 0a 64 65 73 63 72 |$1$. Sin|ce.descr|
|00001d40| 69 70 74 6f 72 73 20 61 | 72 65 20 73 68 61 72 65 |iptors a|re share|
|00001d50| 64 20 62 65 74 77 65 65 | 6e 20 74 68 65 20 70 61 |d betwee|n the pa|
|00001d60| 72 65 6e 74 20 61 6e 64 | 20 63 68 69 6c 64 2c 20 |rent and| child, |
|00001d70| 77 65 20 73 68 6f 75 6c | 64 20 61 6c 77 61 79 73 |we shoul|d always|
|00001d80| 20 62 65 20 73 75 72 65 | 0a 74 6f 20 63 6c 6f 73 | be sure|.to clos|
|00001d90| 65 20 74 68 65 20 65 6e | 64 20 6f 66 20 70 69 70 |e the en|d of pip|
|00001da0| 65 20 77 65 20 61 72 65 | 6e 27 74 20 63 6f 6e 63 |e we are|n't conc|
|00001db0| 65 72 6e 65 64 20 77 69 | 74 68 2e 20 20 4f 6e 20 |erned wi|th. On |
|00001dc0| 61 20 74 65 63 68 6e 69 | 63 61 6c 20 6e 6f 74 65 |a techni|cal note|
|00001dd0| 2c 20 74 68 65 0a 45 4f | 46 20 77 69 6c 6c 20 6e |, the.EO|F will n|
|00001de0| 65 76 65 72 20 62 65 20 | 72 65 74 75 72 6e 65 64 |ever be |returned|
|00001df0| 20 69 66 20 74 68 65 20 | 75 6e 6e 65 63 65 73 73 | if the |unnecess|
|00001e00| 61 72 79 20 65 6e 64 73 | 20 6f 66 20 74 68 65 20 |ary ends| of the |
|00001e10| 70 69 70 65 20 61 72 65 | 20 6e 6f 74 0a 65 78 70 |pipe are| not.exp|
|00001e20| 6c 69 63 69 74 6c 79 20 | 63 6c 6f 73 65 64 2e 0a |licitly |closed..|
|00001e30| 0a 5c 62 65 67 69 6e 7b | 76 65 72 62 61 74 69 6d |.\begin{|verbatim|
|00001e40| 7d 0a 20 20 20 20 20 20 | 20 20 23 69 6e 63 6c 75 |}. | #inclu|
|00001e50| 64 65 20 3c 73 74 64 69 | 6f 2e 68 3e 0a 20 20 20 |de <stdi|o.h>. |
|00001e60| 20 20 20 20 20 23 69 6e | 63 6c 75 64 65 20 3c 75 | #in|clude <u|
|00001e70| 6e 69 73 74 64 2e 68 3e | 0a 20 20 20 20 20 20 20 |nistd.h>|. |
|00001e80| 20 23 69 6e 63 6c 75 64 | 65 20 3c 73 79 73 2f 74 | #includ|e <sys/t|
|00001e90| 79 70 65 73 2e 68 3e 0a | 0a 20 20 20 20 20 20 20 |ypes.h>.|. |
|00001ea0| 20 6d 61 69 6e 28 29 0a | 20 20 20 20 20 20 20 20 | main().| |
|00001eb0| 7b 0a 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |{. | |
|00001ec0| 20 20 69 6e 74 20 20 20 | 20 20 66 64 5b 32 5d 3b | int | fd[2];|
|00001ed0| 0a 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |. | |
|00001ee0| 20 70 69 64 5f 74 20 20 | 20 63 68 69 6c 64 70 69 | pid_t | childpi|
|00001ef0| 64 3b 0a 0a 20 20 20 20 | 20 20 20 20 20 20 20 20 |d;.. | |
|00001f00| 20 20 20 20 70 69 70 65 | 28 66 64 29 3b 0a 20 20 | pipe|(fd);. |
|00001f10| 20 20 20 20 20 20 0a 20 | 20 20 20 20 20 20 20 20 | . | |
|00001f20| 20 20 20 20 20 20 20 69 | 66 28 28 63 68 69 6c 64 | i|f((child|
|00001f30| 70 69 64 20 3d 20 66 6f | 72 6b 28 29 29 20 3d 3d |pid = fo|rk()) ==|
|00001f40| 20 2d 31 29 0a 20 20 20 | 20 20 20 20 20 20 20 20 | -1). | |
|00001f50| 20 20 20 20 20 7b 0a 20 | 20 20 20 20 20 20 20 20 | {. | |
|00001f60| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 70 | | p|
|00001f70| 65 72 72 6f 72 28 22 66 | 6f 72 6b 22 29 3b 0a 20 |error("f|ork");. |
|00001f80| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00001f90| 20 20 20 20 20 20 20 65 | 78 69 74 28 31 29 3b 0a | e|xit(1);.|
|00001fa0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00001fb0| 7d 0a 0a 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |}.. | |
|00001fc0| 20 20 20 69 66 28 63 68 | 69 6c 64 70 69 64 20 3d | if(ch|ildpid =|
|00001fd0| 3d 20 30 29 0a 20 20 20 | 20 20 20 20 20 20 20 20 |= 0). | |
|00001fe0| 20 20 20 20 20 7b 0a 20 | 20 20 20 20 20 20 20 20 | {. | |
|00001ff0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 2f | | /|
|00002000| 2a 20 43 68 69 6c 64 20 | 70 72 6f 63 65 73 73 20 |* Child |process |
|00002010| 63 6c 6f 73 65 73 20 75 | 70 20 69 6e 70 75 74 20 |closes u|p input |
|00002020| 73 69 64 65 20 6f 66 20 | 70 69 70 65 20 2a 2f 0a |side of |pipe */.|
|00002030| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00002040| 20 20 20 20 20 20 20 20 | 63 6c 6f 73 65 28 66 64 | |close(fd|
|00002050| 5b 30 5d 29 3b 0a 20 20 | 20 20 20 20 20 20 20 20 |[0]);. | |
|00002060| 20 20 20 20 20 20 7d 0a | 20 20 20 20 20 20 20 20 | }.| |
|00002070| 20 20 20 20 20 20 20 20 | 65 6c 73 65 0a 20 20 20 | |else. |
|00002080| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 7b 0a 20 | | {. |
|00002090| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000020a0| 20 20 20 20 20 20 20 2f | 2a 20 50 61 72 65 6e 74 | /|* Parent|
|000020b0| 20 70 72 6f 63 65 73 73 | 20 63 6c 6f 73 65 73 20 | process| closes |
|000020c0| 75 70 20 6f 75 74 70 75 | 74 20 73 69 64 65 20 6f |up outpu|t side o|
|000020d0| 66 20 70 69 70 65 20 2a | 2f 0a 20 20 20 20 20 20 |f pipe *|/. |
|000020e0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000020f0| 20 20 63 6c 6f 73 65 28 | 66 64 5b 31 5d 29 3b 0a | close(|fd[1]);.|
|00002100| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00002110| 7d 0a 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |}. | |
|00002120| 20 20 2e 0a 20 20 20 20 | 20 20 20 20 20 20 20 20 | .. | |
|00002130| 20 20 20 20 2e 0a 20 20 | 20 20 20 20 20 20 7d 0a | .. | }.|
|00002140| 5c 65 6e 64 7b 76 65 72 | 62 61 74 69 6d 7d 0a 0a |\end{ver|batim}..|
|00002150| 0a 0a 0a 41 73 20 6d 65 | 6e 74 69 6f 6e 65 64 20 |...As me|ntioned |
|00002160| 70 72 65 76 69 6f 75 73 | 6c 79 2c 20 6f 6e 63 65 |previous|ly, once|
|00002170| 20 74 68 65 20 70 69 70 | 65 6c 69 6e 65 20 68 61 | the pip|eline ha|
|00002180| 73 20 62 65 65 6e 20 65 | 73 74 61 62 6c 69 73 68 |s been e|stablish|
|00002190| 65 64 2c 20 74 68 65 20 | 66 69 6c 65 0a 64 65 73 |ed, the |file.des|
|000021a0| 63 72 69 70 74 6f 72 73 | 20 6d 61 79 20 62 65 20 |criptors| may be |
|000021b0| 74 72 65 61 74 65 64 20 | 6c 69 6b 65 20 64 65 73 |treated |like des|
|000021c0| 63 72 69 70 74 6f 72 73 | 20 74 6f 20 6e 6f 72 6d |criptors| to norm|
|000021d0| 61 6c 20 66 69 6c 65 73 | 2e 0a 0a 5c 62 65 67 69 |al files|...\begi|
|000021e0| 6e 7b 76 65 72 62 61 74 | 69 6d 7d 0a 0a 2f 2a 2a |n{verbat|im}../**|
|000021f0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00002200| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00002210| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00002220| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00002230| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 0a 20 45 78 63 |********|***. Exc|
|00002240| 65 72 70 74 20 66 72 6f | 6d 20 22 4c 69 6e 75 78 |erpt fro|m "Linux|
|00002250| 20 50 72 6f 67 72 61 6d | 6d 65 72 27 73 20 47 75 | Program|mer's Gu|
|00002260| 69 64 65 20 2d 20 43 68 | 61 70 74 65 72 20 36 22 |ide - Ch|apter 6"|
|00002270| 0a 20 28 43 29 6f 70 79 | 72 69 67 68 74 20 31 39 |. (C)opy|right 19|
|00002280| 39 34 2d 31 39 39 35 2c | 20 53 63 6f 74 74 20 42 |94-1995,| Scott B|
|00002290| 75 72 6b 65 74 74 0a 20 | 2a 2a 2a 2a 2a 2a 2a 2a |urkett. |********|
|000022a0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|000022b0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|000022c0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|000022d0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|000022e0| 2a 2a 2a 2a 2a 20 0a 20 | 4d 4f 44 55 4c 45 3a 20 |***** . |MODULE: |
|000022f0| 70 69 70 65 2e 63 0a 20 | 2a 2a 2a 2a 2a 2a 2a 2a |pipe.c. |********|
|00002300| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00002310| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00002320| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00002330| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00002340| 2a 2a 2a 2a 2a 2f 0a 0a | 23 69 6e 63 6c 75 64 65 |*****/..|#include|
|00002350| 20 3c 73 74 64 69 6f 2e | 68 3e 0a 23 69 6e 63 6c | <stdio.|h>.#incl|
|00002360| 75 64 65 20 3c 75 6e 69 | 73 74 64 2e 68 3e 0a 23 |ude <uni|std.h>.#|
|00002370| 69 6e 63 6c 75 64 65 20 | 3c 73 79 73 2f 74 79 70 |include |<sys/typ|
|00002380| 65 73 2e 68 3e 0a 0a 69 | 6e 74 20 6d 61 69 6e 28 |es.h>..i|nt main(|
|00002390| 76 6f 69 64 29 0a 7b 0a | 20 20 20 20 20 20 20 20 |void).{.| |
|000023a0| 69 6e 74 20 20 20 20 20 | 66 64 5b 32 5d 2c 20 6e |int |fd[2], n|
|000023b0| 62 79 74 65 73 3b 0a 20 | 20 20 20 20 20 20 20 70 |bytes;. | p|
|000023c0| 69 64 5f 74 20 20 20 63 | 68 69 6c 64 70 69 64 3b |id_t c|hildpid;|
|000023d0| 0a 20 20 20 20 20 20 20 | 20 63 68 61 72 20 20 20 |. | char |
|000023e0| 20 73 74 72 69 6e 67 5b | 5d 20 3d 20 22 48 65 6c | string[|] = "Hel|
|000023f0| 6c 6f 2c 20 77 6f 72 6c | 64 21 5c 6e 22 3b 0a 20 |lo, worl|d!\n";. |
|00002400| 20 20 20 20 20 20 20 63 | 68 61 72 20 20 20 20 72 | c|har r|
|00002410| 65 61 64 62 75 66 66 65 | 72 5b 38 30 5d 3b 0a 0a |eadbuffe|r[80];..|
|00002420| 20 20 20 20 20 20 20 20 | 70 69 70 65 28 66 64 29 | |pipe(fd)|
|00002430| 3b 0a 20 20 20 20 20 20 | 20 20 0a 20 20 20 20 20 |;. | . |
|00002440| 20 20 20 69 66 28 28 63 | 68 69 6c 64 70 69 64 20 | if((c|hildpid |
|00002450| 3d 20 66 6f 72 6b 28 29 | 29 20 3d 3d 20 2d 31 29 |= fork()|) == -1)|
|00002460| 0a 20 20 20 20 20 20 20 | 20 7b 0a 20 20 20 20 20 |. | {. |
|00002470| 20 20 20 20 20 20 20 20 | 20 20 20 70 65 72 72 6f | | perro|
|00002480| 72 28 22 66 6f 72 6b 22 | 29 3b 0a 20 20 20 20 20 |r("fork"|);. |
|00002490| 20 20 20 20 20 20 20 20 | 20 20 20 65 78 69 74 28 | | exit(|
|000024a0| 31 29 3b 0a 20 20 20 20 | 20 20 20 20 7d 0a 0a 20 |1);. | }.. |
|000024b0| 20 20 20 20 20 20 20 69 | 66 28 63 68 69 6c 64 70 | i|f(childp|
|000024c0| 69 64 20 3d 3d 20 30 29 | 0a 20 20 20 20 20 20 20 |id == 0)|. |
|000024d0| 20 7b 0a 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | {. | |
|000024e0| 20 20 20 2f 2a 20 43 68 | 69 6c 64 20 70 72 6f 63 | /* Ch|ild proc|
|000024f0| 65 73 73 20 63 6c 6f 73 | 65 73 20 75 70 20 69 6e |ess clos|es up in|
|00002500| 70 75 74 20 73 69 64 65 | 20 6f 66 20 70 69 70 65 |put side| of pipe|
|00002510| 20 2a 2f 0a 20 20 20 20 | 20 20 20 20 20 20 20 20 | */. | |
|00002520| 20 20 20 20 63 6c 6f 73 | 65 28 66 64 5b 30 5d 29 | clos|e(fd[0])|
|00002530| 3b 0a 0a 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |;.. | |
|00002540| 20 20 20 2f 2a 20 53 65 | 6e 64 20 22 73 74 72 69 | /* Se|nd "stri|
|00002550| 6e 67 22 20 74 68 72 6f | 75 67 68 20 74 68 65 20 |ng" thro|ugh the |
|00002560| 6f 75 74 70 75 74 20 73 | 69 64 65 20 6f 66 20 70 |output s|ide of p|
|00002570| 69 70 65 20 2a 2f 0a 20 | 20 20 20 20 20 20 20 20 |ipe */. | |
|00002580| 20 20 20 20 20 20 20 77 | 72 69 74 65 28 66 64 5b | w|rite(fd[|
|00002590| 31 5d 2c 20 73 74 72 69 | 6e 67 2c 20 73 74 72 6c |1], stri|ng, strl|
|000025a0| 65 6e 28 73 74 72 69 6e | 67 29 29 3b 0a 20 20 20 |en(strin|g));. |
|000025b0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 65 78 69 | | exi|
|000025c0| 74 28 30 29 3b 0a 20 20 | 20 20 20 20 20 20 7d 0a |t(0);. | }.|
|000025d0| 20 20 20 20 20 20 20 20 | 65 6c 73 65 0a 20 20 20 | |else. |
|000025e0| 20 20 20 20 20 7b 0a 20 | 20 20 20 20 20 20 20 20 | {. | |
|000025f0| 20 20 20 20 20 20 20 2f | 2a 20 50 61 72 65 6e 74 | /|* Parent|
|00002600| 20 70 72 6f 63 65 73 73 | 20 63 6c 6f 73 65 73 20 | process| closes |
|00002610| 75 70 20 6f 75 74 70 75 | 74 20 73 69 64 65 20 6f |up outpu|t side o|
|00002620| 66 20 70 69 70 65 20 2a | 2f 0a 20 20 20 20 20 20 |f pipe *|/. |
|00002630| 20 20 20 20 20 20 20 20 | 20 20 63 6c 6f 73 65 28 | | close(|
|00002640| 66 64 5b 31 5d 29 3b 0a | 0a 20 20 20 20 20 20 20 |fd[1]);.|. |
|00002650| 20 20 20 20 20 20 20 20 | 20 2f 2a 20 52 65 61 64 | | /* Read|
|00002660| 20 69 6e 20 61 20 73 74 | 72 69 6e 67 20 66 72 6f | in a st|ring fro|
|00002670| 6d 20 74 68 65 20 70 69 | 70 65 20 2a 2f 0a 20 20 |m the pi|pe */. |
|00002680| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 6e 62 | | nb|
|00002690| 79 74 65 73 20 3d 20 72 | 65 61 64 28 66 64 5b 30 |ytes = r|ead(fd[0|
|000026a0| 5d 2c 20 72 65 61 64 62 | 75 66 66 65 72 2c 20 73 |], readb|uffer, s|
|000026b0| 69 7a 65 6f 66 28 72 65 | 61 64 62 75 66 66 65 72 |izeof(re|adbuffer|
|000026c0| 29 29 3b 0a 20 20 20 20 | 20 20 20 20 20 20 20 20 |));. | |
|000026d0| 20 20 20 20 70 72 69 6e | 74 66 28 22 52 65 63 65 | prin|tf("Rece|
|000026e0| 69 76 65 64 20 73 74 72 | 69 6e 67 3a 20 25 73 22 |ived str|ing: %s"|
|000026f0| 2c 20 72 65 61 64 62 75 | 66 66 65 72 29 3b 0a 20 |, readbu|ffer);. |
|00002700| 20 20 20 20 20 20 20 7d | 0a 20 20 20 20 20 20 20 | }|. |
|00002710| 20 0a 20 20 20 20 20 20 | 20 20 72 65 74 75 72 6e | . | return|
|00002720| 28 30 29 3b 0a 7d 0a 5c | 65 6e 64 7b 76 65 72 62 |(0);.}.\|end{verb|
|00002730| 61 74 69 6d 7d 0a 0a 0a | 0a 0a 4f 66 74 65 6e 2c |atim}...|..Often,|
|00002740| 20 74 68 65 20 64 65 73 | 63 72 69 70 74 6f 72 73 | the des|criptors|
|00002750| 20 69 6e 20 74 68 65 20 | 63 68 69 6c 64 20 61 72 | in the |child ar|
|00002760| 65 20 64 75 70 6c 69 63 | 61 74 65 64 20 6f 6e 74 |e duplic|ated ont|
|00002770| 6f 20 73 74 61 6e 64 61 | 72 64 20 69 6e 70 75 74 |o standa|rd input|
|00002780| 20 0a 6f 72 20 6f 75 74 | 70 75 74 2e 20 20 54 68 | .or out|put. Th|
|00002790| 65 20 63 68 69 6c 64 20 | 63 61 6e 20 74 68 65 6e |e child |can then|
|000027a0| 20 65 78 65 63 28 29 20 | 61 6e 6f 74 68 65 72 20 | exec() |another |
|000027b0| 70 72 6f 67 72 61 6d 2c | 20 77 68 69 63 68 20 69 |program,| which i|
|000027c0| 6e 68 65 72 69 74 73 20 | 74 68 65 0a 73 74 61 6e |nherits |the.stan|
|000027d0| 64 61 72 64 20 73 74 72 | 65 61 6d 73 2e 20 20 4c |dard str|eams. L|
|000027e0| 65 74 27 73 20 6c 6f 6f | 6b 20 61 74 20 74 68 65 |et's loo|k at the|
|000027f0| 20 64 75 70 28 29 20 73 | 79 73 74 65 6d 20 63 61 | dup() s|ystem ca|
|00002800| 6c 6c 3a 0a 0a 0a 0a 5c | 70 61 72 0a 5c 61 64 64 |ll:....\|par.\add|
|00002810| 76 73 70 61 63 65 7b 5c | 6d 65 64 73 6b 69 70 61 |vspace{\|medskipa|
|00002820| 6d 6f 75 6e 74 7d 0a 5c | 6e 6f 70 61 67 65 62 72 |mount}.\|nopagebr|
|00002830| 65 61 6b 5c 68 72 75 6c | 65 0a 5c 62 65 67 69 6e |eak\hrul|e.\begin|
|00002840| 7b 76 65 72 62 61 74 69 | 6d 7d 0a 20 20 53 59 53 |{verbati|m}. SYS|
|00002850| 54 45 4d 20 43 41 4c 4c | 3a 20 64 75 70 28 29 3b |TEM CALL|: dup();|
|00002860| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00002870| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00002880| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00002890| 20 20 20 20 20 20 20 20 | 20 20 20 0a 0a 20 20 50 | | .. P|
|000028a0| 52 4f 54 4f 54 59 50 45 | 3a 20 69 6e 74 20 64 75 |ROTOTYPE|: int du|
|000028b0| 70 28 20 69 6e 74 20 6f | 6c 64 66 64 20 29 3b 20 |p( int o|ldfd ); |
|000028c0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000028d0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000028e0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 0a 20 20 | | . |
|000028f0| 20 20 52 45 54 55 52 4e | 53 3a 20 6e 65 77 20 64 | RETURN|S: new d|
|00002900| 65 73 63 72 69 70 74 6f | 72 20 6f 6e 20 73 75 63 |escripto|r on suc|
|00002910| 63 65 73 73 20 20 20 20 | 20 20 20 20 20 20 20 20 |cess | |
|00002920| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00002930| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 0a 20 | | . |
|00002940| 20 20 20 20 20 20 20 20 | 20 20 20 20 2d 31 20 6f | | -1 o|
|00002950| 6e 20 65 72 72 6f 72 3a | 20 65 72 72 6e 6f 20 3d |n error:| errno =|
|00002960| 20 45 42 41 44 46 20 28 | 6f 6c 64 66 64 20 69 73 | EBADF (|oldfd is|
|00002970| 20 6e 6f 74 20 61 20 76 | 61 6c 69 64 20 64 65 73 | not a v|alid des|
|00002980| 63 72 69 70 74 6f 72 29 | 20 20 20 20 20 20 20 0a |criptor)| .|
|00002990| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000029a0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000029b0| 20 20 45 42 41 44 46 20 | 28 6e 65 77 66 64 20 69 | EBADF |(newfd i|
|000029c0| 73 20 6f 75 74 20 6f 66 | 20 72 61 6e 67 65 29 20 |s out of| range) |
|000029d0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000029e0| 0a 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |. | |
|000029f0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00002a00| 20 20 20 45 4d 46 49 4c | 45 20 28 74 6f 6f 20 6d | EMFIL|E (too m|
|00002a10| 61 6e 79 20 64 65 73 63 | 72 69 70 74 6f 72 73 20 |any desc|riptors |
|00002a20| 66 6f 72 20 74 68 65 20 | 70 72 6f 63 65 73 73 29 |for the |process)|
|00002a30| 20 0a 0a 20 20 4e 4f 54 | 45 53 3a 20 74 68 65 20 | .. NOT|ES: the |
|00002a40| 6f 6c 64 20 64 65 73 63 | 72 69 70 74 6f 72 20 69 |old desc|riptor i|
|00002a50| 73 20 6e 6f 74 20 63 6c | 6f 73 65 64 21 20 20 42 |s not cl|osed! B|
|00002a60| 6f 74 68 20 6d 61 79 20 | 62 65 20 75 73 65 64 20 |oth may |be used |
|00002a70| 69 6e 74 65 72 63 68 61 | 6e 67 65 61 62 6c 79 20 |intercha|ngeably |
|00002a80| 20 20 20 0a 5c 65 6e 64 | 7b 76 65 72 62 61 74 69 | .\end|{verbati|
|00002a90| 6d 7d 20 0a 5c 6e 6f 70 | 61 67 65 62 72 65 61 6b |m} .\nop|agebreak|
|00002aa0| 5c 68 72 75 6c 65 20 0a | 5c 61 64 64 76 73 70 61 |\hrule .|\addvspa|
|00002ab0| 63 65 7b 5c 6d 65 64 73 | 6b 69 70 61 6d 6f 75 6e |ce{\meds|kipamoun|
|00002ac0| 74 7d 0a 0a 0a 0a 0a 0a | 0a 41 6c 74 68 6f 75 67 |t}......|.Althoug|
|00002ad0| 68 20 74 68 65 20 6f 6c | 64 20 64 65 73 63 72 69 |h the ol|d descri|
|00002ae0| 70 74 6f 72 20 61 6e 64 | 20 74 68 65 20 6e 65 77 |ptor and| the new|
|00002af0| 6c 79 20 63 72 65 61 74 | 65 64 20 64 65 73 63 72 |ly creat|ed descr|
|00002b00| 69 70 74 6f 72 20 63 61 | 6e 20 62 65 20 75 73 65 |iptor ca|n be use|
|00002b10| 64 0a 69 6e 74 65 72 63 | 68 61 6e 67 65 61 62 6c |d.interc|hangeabl|
|00002b20| 79 2c 20 77 65 20 77 69 | 6c 6c 20 74 79 70 69 63 |y, we wi|ll typic|
|00002b30| 61 6c 6c 79 20 63 6c 6f | 73 65 20 6f 6e 65 20 6f |ally clo|se one o|
|00002b40| 66 20 74 68 65 20 73 74 | 61 6e 64 61 72 64 20 73 |f the st|andard s|
|00002b50| 74 72 65 61 6d 73 20 66 | 69 72 73 74 2e 0a 54 68 |treams f|irst..Th|
|00002b60| 65 20 64 75 70 28 29 20 | 73 79 73 74 65 6d 20 63 |e dup() |system c|
|00002b70| 61 6c 6c 20 75 73 65 73 | 20 74 68 65 20 6c 6f 77 |all uses| the low|
|00002b80| 65 73 74 2d 6e 75 6d 62 | 65 72 65 64 2c 20 75 6e |est-numb|ered, un|
|00002b90| 75 73 65 64 20 64 65 73 | 63 72 69 70 74 6f 72 20 |used des|criptor |
|00002ba0| 66 6f 72 20 74 68 65 0a | 6e 65 77 20 6f 6e 65 2e |for the.|new one.|
|00002bb0| 0a 0a 43 6f 6e 73 69 64 | 65 72 3a 0a 0a 5c 62 65 |..Consid|er:..\be|
|00002bc0| 67 69 6e 7b 76 65 72 62 | 61 74 69 6d 7d 0a 20 20 |gin{verb|atim}. |
|00002bd0| 20 20 20 20 20 20 2e 0a | 20 20 20 20 20 20 20 20 | ..| |
|00002be0| 2e 0a 20 20 20 20 20 20 | 20 20 63 68 69 6c 64 70 |.. | childp|
|00002bf0| 69 64 20 3d 20 66 6f 72 | 6b 28 29 3b 0a 20 20 20 |id = for|k();. |
|00002c00| 20 20 20 20 20 0a 20 20 | 20 20 20 20 20 20 69 66 | . | if|
|00002c10| 28 63 68 69 6c 64 70 69 | 64 20 3d 3d 20 30 29 0a |(childpi|d == 0).|
|00002c20| 20 20 20 20 20 20 20 20 | 7b 0a 20 20 20 20 20 20 | |{. |
|00002c30| 20 20 20 20 20 20 20 20 | 20 20 2f 2a 20 43 6c 6f | | /* Clo|
|00002c40| 73 65 20 75 70 20 73 74 | 61 6e 64 61 72 64 20 69 |se up st|andard i|
|00002c50| 6e 70 75 74 20 6f 66 20 | 74 68 65 20 63 68 69 6c |nput of |the chil|
|00002c60| 64 20 2a 2f 0a 20 20 20 | 20 20 20 20 20 20 20 20 |d */. | |
|00002c70| 20 20 20 20 20 63 6c 6f | 73 65 28 30 29 3b 0a 20 | clo|se(0);. |
|00002c80| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 0a | | .|
|00002c90| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00002ca0| 2f 2a 20 44 75 70 6c 69 | 63 61 74 65 20 74 68 65 |/* Dupli|cate the|
|00002cb0| 20 69 6e 70 75 74 20 73 | 69 64 65 20 6f 66 20 70 | input s|ide of p|
|00002cc0| 69 70 65 20 74 6f 20 73 | 74 64 69 6e 20 2a 2f 0a |ipe to s|tdin */.|
|00002cd0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00002ce0| 64 75 70 28 66 64 5b 30 | 5d 29 3b 0a 20 20 20 20 |dup(fd[0|]);. |
|00002cf0| 20 20 20 20 20 20 20 20 | 20 20 20 20 65 78 65 63 | | exec|
|00002d00| 6c 70 28 22 73 6f 72 74 | 22 2c 20 22 73 6f 72 74 |lp("sort|", "sort|
|00002d10| 22 2c 20 4e 55 4c 4c 29 | 3b 0a 20 20 20 20 20 20 |", NULL)|;. |
|00002d20| 20 20 20 20 20 20 20 20 | 20 20 2e 0a 20 20 20 20 | | .. |
|00002d30| 20 20 20 20 7d 0a 5c 65 | 6e 64 7b 76 65 72 62 61 | }.\e|nd{verba|
|00002d40| 74 69 6d 7d 0a 0a 0a 0a | 0a 53 69 6e 63 65 20 66 |tim}....|.Since f|
|00002d50| 69 6c 65 20 64 65 73 63 | 72 69 70 74 6f 72 20 30 |ile desc|riptor 0|
|00002d60| 20 28 73 74 64 69 6e 29 | 20 77 61 73 20 63 6c 6f | (stdin)| was clo|
|00002d70| 73 65 64 2c 20 74 68 65 | 20 63 61 6c 6c 20 74 6f |sed, the| call to|
|00002d80| 20 64 75 70 28 29 20 64 | 75 70 6c 69 63 61 74 65 | dup() d|uplicate|
|00002d90| 64 20 74 68 65 20 0a 69 | 6e 70 75 74 20 64 65 73 |d the .i|nput des|
|00002da0| 63 72 69 70 74 6f 72 20 | 6f 66 20 74 68 65 20 70 |criptor |of the p|
|00002db0| 69 70 65 20 28 66 64 24 | 30 24 29 20 6f 6e 74 6f |ipe (fd$|0$) onto|
|00002dc0| 20 69 74 73 20 73 74 61 | 6e 64 61 72 64 20 69 6e | its sta|ndard in|
|00002dd0| 70 75 74 2e 20 20 57 65 | 20 74 68 65 6e 20 6d 61 |put. We| then ma|
|00002de0| 6b 65 0a 61 20 63 61 6c | 6c 20 74 6f 20 65 78 65 |ke.a cal|l to exe|
|00002df0| 63 6c 70 28 29 2c 20 74 | 6f 20 6f 76 65 72 6c 61 |clp(), t|o overla|
|00002e00| 79 20 74 68 65 20 63 68 | 69 6c 64 27 73 20 74 65 |y the ch|ild's te|
|00002e10| 78 74 20 73 65 67 6d 65 | 6e 74 20 28 63 6f 64 65 |xt segme|nt (code|
|00002e20| 29 20 77 69 74 68 20 74 | 68 61 74 20 6f 66 0a 74 |) with t|hat of.t|
|00002e30| 68 65 20 73 6f 72 74 20 | 70 72 6f 67 72 61 6d 2e |he sort |program.|
|00002e40| 20 20 53 69 6e 63 65 20 | 6e 65 77 6c 79 20 65 78 | Since |newly ex|
|00002e50| 65 63 27 64 20 70 72 6f | 67 72 61 6d 73 20 69 6e |ec'd pro|grams in|
|00002e60| 68 65 72 69 74 20 73 74 | 61 6e 64 61 72 64 20 73 |herit st|andard s|
|00002e70| 74 72 65 61 6d 73 20 66 | 72 6f 6d 0a 74 68 65 69 |treams f|rom.thei|
|00002e80| 72 20 73 70 61 77 6e 65 | 72 73 2c 20 69 74 20 61 |r spawne|rs, it a|
|00002e90| 63 74 75 61 6c 6c 79 20 | 69 6e 68 65 72 69 74 73 |ctually |inherits|
|00002ea0| 20 74 68 65 20 69 6e 70 | 75 74 20 73 69 64 65 20 | the inp|ut side |
|00002eb0| 6f 66 20 74 68 65 20 70 | 69 70 65 20 61 73 20 69 |of the p|ipe as i|
|00002ec0| 74 73 0a 73 74 61 6e 64 | 61 72 64 20 69 6e 70 75 |ts.stand|ard inpu|
|00002ed0| 74 21 20 20 4e 6f 77 2c | 20 61 6e 79 74 68 69 6e |t! Now,| anythin|
|00002ee0| 67 20 74 68 61 74 20 74 | 68 65 20 6f 72 69 67 69 |g that t|he origi|
|00002ef0| 6e 61 6c 20 70 61 72 65 | 6e 74 20 70 72 6f 63 65 |nal pare|nt proce|
|00002f00| 73 73 20 73 65 6e 64 73 | 20 74 6f 20 74 68 65 0a |ss sends| to the.|
|00002f10| 70 69 70 65 2c 20 67 6f | 65 73 20 69 6e 74 6f 20 |pipe, go|es into |
|00002f20| 74 68 65 20 73 6f 72 74 | 20 66 61 63 69 6c 69 74 |the sort| facilit|
|00002f30| 79 2e 0a 0a 54 68 65 72 | 65 20 69 73 20 61 6e 6f |y...Ther|e is ano|
|00002f40| 74 68 65 72 20 73 79 73 | 74 65 6d 20 63 61 6c 6c |ther sys|tem call|
|00002f50| 2c 20 64 75 70 32 28 29 | 2c 20 77 68 69 63 68 20 |, dup2()|, which |
|00002f60| 63 61 6e 20 62 65 20 75 | 73 65 64 20 61 73 20 77 |can be u|sed as w|
|00002f70| 65 6c 6c 2e 20 20 54 68 | 69 73 20 0a 70 61 72 74 |ell. Th|is .part|
|00002f80| 69 63 75 6c 61 72 20 63 | 61 6c 6c 20 6f 72 69 67 |icular c|all orig|
|00002f90| 69 6e 61 74 65 64 20 77 | 69 74 68 20 56 65 72 73 |inated w|ith Vers|
|00002fa0| 69 6f 6e 20 37 20 6f 66 | 20 55 4e 49 58 2c 20 61 |ion 7 of| UNIX, a|
|00002fb0| 6e 64 20 77 61 73 20 63 | 61 72 72 69 65 64 20 6f |nd was c|arried o|
|00002fc0| 6e 20 74 68 72 6f 75 67 | 68 0a 74 68 65 20 42 53 |n throug|h.the BS|
|00002fd0| 44 20 72 65 6c 65 61 73 | 65 73 20 61 6e 64 20 69 |D releas|es and i|
|00002fe0| 73 20 6e 6f 77 20 72 65 | 71 75 69 72 65 64 20 62 |s now re|quired b|
|00002ff0| 79 20 74 68 65 20 50 4f | 53 49 58 20 73 74 61 6e |y the PO|SIX stan|
|00003000| 64 61 72 64 2e 20 0a 0a | 0a 0a 5c 70 61 72 0a 5c |dard. ..|..\par.\|
|00003010| 61 64 64 76 73 70 61 63 | 65 7b 5c 6d 65 64 73 6b |addvspac|e{\medsk|
|00003020| 69 70 61 6d 6f 75 6e 74 | 7d 0a 5c 6e 6f 70 61 67 |ipamount|}.\nopag|
|00003030| 65 62 72 65 61 6b 5c 68 | 72 75 6c 65 0a 5c 62 65 |ebreak\h|rule.\be|
|00003040| 67 69 6e 7b 76 65 72 62 | 61 74 69 6d 7d 0a 20 20 |gin{verb|atim}. |
|00003050| 53 59 53 54 45 4d 20 43 | 41 4c 4c 3a 20 64 75 70 |SYSTEM C|ALL: dup|
|00003060| 32 28 29 3b 20 20 20 20 | 20 20 20 20 20 20 20 20 |2(); | |
|00003070| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00003080| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00003090| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 0a 0a | | ..|
|000030a0| 20 20 50 52 4f 54 4f 54 | 59 50 45 3a 20 69 6e 74 | PROTOT|YPE: int|
|000030b0| 20 64 75 70 32 28 20 69 | 6e 74 20 6f 6c 64 66 64 | dup2( i|nt oldfd|
|000030c0| 2c 20 69 6e 74 20 6e 65 | 77 66 64 20 29 3b 20 20 |, int ne|wfd ); |
|000030d0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000030e0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000030f0| 0a 20 20 20 20 52 45 54 | 55 52 4e 53 3a 20 6e 65 |. RET|URNS: ne|
|00003100| 77 20 64 65 73 63 72 69 | 70 74 6f 72 20 6f 6e 20 |w descri|ptor on |
|00003110| 73 75 63 63 65 73 73 20 | 20 20 20 20 20 20 20 20 |success | |
|00003120| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00003130| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00003140| 20 0a 20 20 20 20 20 20 | 20 20 20 20 20 20 20 2d | . | -|
|00003150| 31 20 6f 6e 20 65 72 72 | 6f 72 3a 20 65 72 72 6e |1 on err|or: errn|
|00003160| 6f 20 3d 20 45 42 41 44 | 46 20 28 6f 6c 64 66 64 |o = EBAD|F (oldfd|
|00003170| 20 69 73 20 6e 6f 74 20 | 61 20 76 61 6c 69 64 20 | is not |a valid |
|00003180| 64 65 73 63 72 69 70 74 | 6f 72 29 20 20 20 20 20 |descript|or) |
|00003190| 20 20 0a 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | . | |
|000031a0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000031b0| 20 20 20 20 20 45 42 41 | 44 46 20 28 6e 65 77 66 | EBA|DF (newf|
|000031c0| 64 20 69 73 20 6f 75 74 | 20 6f 66 20 72 61 6e 67 |d is out| of rang|
|000031d0| 65 29 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |e) | |
|000031e0| 20 20 20 0a 20 20 20 20 | 20 20 20 20 20 20 20 20 | . | |
|000031f0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00003200| 20 20 20 20 20 20 45 4d | 46 49 4c 45 20 28 74 6f | EM|FILE (to|
|00003210| 6f 20 6d 61 6e 79 20 64 | 65 73 63 72 69 70 74 6f |o many d|escripto|
|00003220| 72 73 20 66 6f 72 20 74 | 68 65 20 70 72 6f 63 65 |rs for t|he proce|
|00003230| 73 73 29 20 0a 0a 20 20 | 4e 4f 54 45 53 3a 20 74 |ss) .. |NOTES: t|
|00003240| 68 65 20 6f 6c 64 20 64 | 65 73 63 72 69 70 74 6f |he old d|escripto|
|00003250| 72 20 69 73 20 63 6c 6f | 73 65 64 20 77 69 74 68 |r is clo|sed with|
|00003260| 20 64 75 70 32 28 29 21 | 20 20 20 20 20 20 20 20 | dup2()!| |
|00003270| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00003280| 20 20 20 20 20 20 0a 5c | 65 6e 64 7b 76 65 72 62 | .\|end{verb|
|00003290| 61 74 69 6d 7d 20 0a 5c | 6e 6f 70 61 67 65 62 72 |atim} .\|nopagebr|
|000032a0| 65 61 6b 5c 68 72 75 6c | 65 20 0a 5c 61 64 64 76 |eak\hrul|e .\addv|
|000032b0| 73 70 61 63 65 7b 5c 6d | 65 64 73 6b 69 70 61 6d |space{\m|edskipam|
|000032c0| 6f 75 6e 74 7d 0a 0a 0a | 0a 0a 57 69 74 68 20 74 |ount}...|..With t|
|000032d0| 68 69 73 20 70 61 72 74 | 69 63 75 6c 61 72 20 63 |his part|icular c|
|000032e0| 61 6c 6c 2c 20 77 65 20 | 68 61 76 65 20 74 68 65 |all, we |have the|
|000032f0| 20 63 6c 6f 73 65 20 6f | 70 65 72 61 74 69 6f 6e | close o|peration|
|00003300| 2c 20 61 6e 64 20 74 68 | 65 20 61 63 74 75 61 6c |, and th|e actual|
|00003310| 0a 64 65 73 63 72 69 70 | 74 6f 72 20 64 75 70 6c |.descrip|tor dupl|
|00003320| 69 63 61 74 69 6f 6e 2c | 20 77 72 61 70 70 65 64 |ication,| wrapped|
|00003330| 20 75 70 20 69 6e 20 6f | 6e 65 20 73 79 73 74 65 | up in o|ne syste|
|00003340| 6d 20 63 61 6c 6c 2e 20 | 20 49 6e 20 61 64 64 69 |m call. | In addi|
|00003350| 74 69 6f 6e 2c 20 69 74 | 0a 69 73 20 67 75 61 72 |tion, it|.is guar|
|00003360| 61 6e 74 65 65 64 20 74 | 6f 20 62 65 20 61 74 6f |anteed t|o be ato|
|00003370| 6d 69 63 2c 20 77 68 69 | 63 68 20 65 73 73 65 6e |mic, whi|ch essen|
|00003380| 74 69 61 6c 6c 79 20 6d | 65 61 6e 73 20 74 68 61 |tially m|eans tha|
|00003390| 74 20 69 74 20 77 69 6c | 6c 20 6e 65 76 65 72 0a |t it wil|l never.|
|000033a0| 62 65 20 69 6e 74 65 72 | 72 75 70 74 65 64 20 62 |be inter|rupted b|
|000033b0| 79 20 61 6e 20 61 72 72 | 69 76 69 6e 67 20 73 69 |y an arr|iving si|
|000033c0| 67 6e 61 6c 2e 20 20 54 | 68 65 20 65 6e 74 69 72 |gnal. T|he entir|
|000033d0| 65 20 6f 70 65 72 61 74 | 69 6f 6e 20 77 69 6c 6c |e operat|ion will|
|000033e0| 20 74 72 61 6e 73 70 69 | 72 65 0a 62 65 66 6f 72 | transpi|re.befor|
|000033f0| 65 20 72 65 74 75 72 6e | 69 6e 67 20 63 6f 6e 74 |e return|ing cont|
|00003400| 72 6f 6c 20 74 6f 20 74 | 68 65 20 6b 65 72 6e 65 |rol to t|he kerne|
|00003410| 6c 20 66 6f 72 20 73 69 | 67 6e 61 6c 20 64 69 73 |l for si|gnal dis|
|00003420| 70 61 74 63 68 69 6e 67 | 2e 20 20 57 69 74 68 20 |patching|. With |
|00003430| 74 68 65 0a 6f 72 69 67 | 69 6e 61 6c 20 64 75 70 |the.orig|inal dup|
|00003440| 28 29 20 73 79 73 74 65 | 6d 20 63 61 6c 6c 2c 20 |() syste|m call, |
|00003450| 70 72 6f 67 72 61 6d 6d | 65 72 73 20 68 61 64 20 |programm|ers had |
|00003460| 74 6f 20 70 65 72 66 6f | 72 6d 20 61 20 63 6c 6f |to perfo|rm a clo|
|00003470| 73 65 28 29 20 6f 70 65 | 72 61 74 69 6f 6e 0a 62 |se() ope|ration.b|
|00003480| 65 66 6f 72 65 20 63 61 | 6c 6c 69 6e 67 20 69 74 |efore ca|lling it|
|00003490| 2e 20 20 54 68 61 74 20 | 72 65 73 75 6c 74 65 64 |. That |resulted|
|000034a0| 20 69 6e 20 74 77 6f 20 | 73 79 73 74 65 6d 20 63 | in two |system c|
|000034b0| 61 6c 6c 73 2c 20 77 69 | 74 68 20 61 20 73 6d 61 |alls, wi|th a sma|
|000034c0| 6c 6c 20 64 65 67 72 65 | 65 0a 6f 66 20 76 75 6c |ll degre|e.of vul|
|000034d0| 6e 65 72 61 62 69 6c 69 | 74 79 20 69 6e 20 74 68 |nerabili|ty in th|
|000034e0| 65 20 62 72 69 65 66 20 | 61 6d 6f 75 6e 74 20 6f |e brief |amount o|
|000034f0| 66 20 74 69 6d 65 20 77 | 68 69 63 68 20 65 6c 61 |f time w|hich ela|
|00003500| 70 73 65 64 20 62 65 74 | 77 65 65 6e 20 74 68 65 |psed bet|ween the|
|00003510| 6d 2e 20 20 0a 49 66 20 | 61 20 73 69 67 6e 61 6c |m. .If |a signal|
|00003520| 20 61 72 72 69 76 65 64 | 20 64 75 72 69 6e 67 20 | arrived| during |
|00003530| 74 68 61 74 20 62 72 69 | 65 66 20 69 6e 73 74 61 |that bri|ef insta|
|00003540| 6e 63 65 2c 20 74 68 65 | 20 64 65 73 63 72 69 70 |nce, the| descrip|
|00003550| 74 6f 72 20 64 75 70 6c | 69 63 61 74 69 6f 6e 0a |tor dupl|ication.|
|00003560| 77 6f 75 6c 64 20 66 61 | 69 6c 2e 20 20 4f 66 20 |would fa|il. Of |
|00003570| 63 6f 75 72 73 65 2c 20 | 64 75 70 32 28 29 20 73 |course, |dup2() s|
|00003580| 6f 6c 76 65 73 20 74 68 | 69 73 20 70 72 6f 62 6c |olves th|is probl|
|00003590| 65 6d 20 66 6f 72 20 75 | 73 2e 0a 0a 43 6f 6e 73 |em for u|s...Cons|
|000035a0| 69 64 65 72 3a 0a 0a 5c | 62 65 67 69 6e 7b 76 65 |ider:..\|begin{ve|
|000035b0| 72 62 61 74 69 6d 7d 0a | 20 20 20 20 20 20 20 20 |rbatim}.| |
|000035c0| 2e 0a 20 20 20 20 20 20 | 20 20 2e 0a 20 20 20 20 |.. | .. |
|000035d0| 20 20 20 20 63 68 69 6c | 64 70 69 64 20 3d 20 66 | chil|dpid = f|
|000035e0| 6f 72 6b 28 29 3b 0a 20 | 20 20 20 20 20 20 20 0a |ork();. | .|
|000035f0| 20 20 20 20 20 20 20 20 | 69 66 28 63 68 69 6c 64 | |if(child|
|00003600| 70 69 64 20 3d 3d 20 30 | 29 0a 20 20 20 20 20 20 |pid == 0|). |
|00003610| 20 20 7b 0a 20 20 20 20 | 20 20 20 20 20 20 20 20 | {. | |
|00003620| 20 20 20 20 2f 2a 20 43 | 6c 6f 73 65 20 73 74 64 | /* C|lose std|
|00003630| 69 6e 2c 20 64 75 70 6c | 69 63 61 74 65 20 74 68 |in, dupl|icate th|
|00003640| 65 20 69 6e 70 75 74 20 | 73 69 64 65 20 6f 66 20 |e input |side of |
|00003650| 70 69 70 65 20 74 6f 20 | 73 74 64 69 6e 20 2a 2f |pipe to |stdin */|
|00003660| 0a 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |. | |
|00003670| 20 64 75 70 32 28 30 2c | 20 66 64 5b 30 5d 29 3b | dup2(0,| fd[0]);|
|00003680| 0a 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |. | |
|00003690| 20 65 78 65 63 6c 70 28 | 22 73 6f 72 74 22 2c 20 | execlp(|"sort", |
|000036a0| 22 73 6f 72 74 22 2c 20 | 4e 55 4c 4c 29 3b 0a 20 |"sort", |NULL);. |
|000036b0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 2e | | .|
|000036c0| 0a 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |. | |
|000036d0| 20 2e 0a 20 20 20 20 20 | 20 20 20 7d 0a 5c 65 6e | .. | }.\en|
|000036e0| 64 7b 76 65 72 62 61 74 | 69 6d 7d 0a 0a 0a 0a 0a |d{verbat|im}.....|
|000036f0| 0a 0a 0a 5c 73 75 62 73 | 65 63 74 69 6f 6e 7b 50 |...\subs|ection{P|
|00003700| 69 70 65 73 20 74 68 65 | 20 45 61 73 79 20 57 61 |ipes the| Easy Wa|
|00003710| 79 21 7d 0a 0a 0a 0a 49 | 66 20 61 6c 6c 20 6f 66 |y!}....I|f all of|
|00003720| 20 74 68 65 20 61 62 6f | 76 65 20 72 61 6d 62 6c | the abo|ve rambl|
|00003730| 69 6e 67 73 20 73 65 65 | 6d 20 6c 69 6b 65 20 61 |ings see|m like a|
|00003740| 20 76 65 72 79 20 72 6f | 75 6e 64 2d 61 62 6f 75 | very ro|und-abou|
|00003750| 74 20 77 61 79 20 6f 66 | 20 63 72 65 61 74 69 6e |t way of| creatin|
|00003760| 67 0a 61 6e 64 20 75 74 | 69 6c 69 7a 69 6e 67 20 |g.and ut|ilizing |
|00003770| 70 69 70 65 73 2c 20 74 | 68 65 72 65 20 69 73 20 |pipes, t|here is |
|00003780| 61 6e 20 61 6c 74 65 72 | 6e 61 74 69 76 65 2e 20 |an alter|native. |
|00003790| 20 0a 0a 0a 0a 5c 70 61 | 72 0a 5c 61 64 64 76 73 | ....\pa|r.\addvs|
|000037a0| 70 61 63 65 7b 5c 6d 65 | 64 73 6b 69 70 61 6d 6f |pace{\me|dskipamo|
|000037b0| 75 6e 74 7d 0a 5c 6e 6f | 70 61 67 65 62 72 65 61 |unt}.\no|pagebrea|
|000037c0| 6b 5c 68 72 75 6c 65 0a | 5c 62 65 67 69 6e 7b 76 |k\hrule.|\begin{v|
|000037d0| 65 72 62 61 74 69 6d 7d | 0a 20 20 4c 49 42 52 41 |erbatim}|. LIBRA|
|000037e0| 52 59 20 46 55 4e 43 54 | 49 4f 4e 3a 20 70 6f 70 |RY FUNCT|ION: pop|
|000037f0| 65 6e 28 29 3b 20 20 20 | 20 20 20 20 20 20 20 20 |en(); | |
|00003800| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00003810| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00003820| 20 20 20 20 20 20 20 20 | 20 0a 0a 20 20 50 52 4f | | .. PRO|
|00003830| 54 4f 54 59 50 45 3a 20 | 46 49 4c 45 20 2a 70 6f |TOTYPE: |FILE *po|
|00003840| 70 65 6e 20 28 20 63 68 | 61 72 20 2a 63 6f 6d 6d |pen ( ch|ar *comm|
|00003850| 61 6e 64 2c 20 63 68 61 | 72 20 2a 74 79 70 65 29 |and, cha|r *type)|
|00003860| 3b 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |; | |
|00003870| 20 20 20 20 20 20 20 20 | 20 20 20 0a 20 20 20 20 | | . |
|00003880| 52 45 54 55 52 4e 53 3a | 20 6e 65 77 20 66 69 6c |RETURNS:| new fil|
|00003890| 65 20 73 74 72 65 61 6d | 20 6f 6e 20 73 75 63 63 |e stream| on succ|
|000038a0| 65 73 73 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |ess | |
|000038b0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000038c0| 20 20 20 20 20 20 20 20 | 20 20 20 20 0a 20 20 20 | | . |
|000038d0| 20 20 20 20 20 20 20 20 | 20 20 4e 55 4c 4c 20 6f | | NULL o|
|000038e0| 6e 20 75 6e 73 75 63 63 | 65 73 73 66 75 6c 20 66 |n unsucc|essful f|
|000038f0| 6f 72 6b 28 29 20 6f 72 | 20 70 69 70 65 28 29 20 |ork() or| pipe() |
|00003900| 63 61 6c 6c 20 20 20 20 | 20 20 20 20 20 20 20 20 |call | |
|00003910| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 0a 0a 20 | | .. |
|00003920| 20 4e 4f 54 45 53 3a 20 | 63 72 65 61 74 65 73 20 | NOTES: |creates |
|00003930| 61 20 70 69 70 65 2c 20 | 61 6e 64 20 70 65 72 66 |a pipe, |and perf|
|00003940| 6f 72 6d 73 20 66 6f 72 | 6b 2f 65 78 65 63 20 6f |orms for|k/exec o|
|00003950| 70 65 72 61 74 69 6f 6e | 73 20 75 73 69 6e 67 20 |peration|s using |
|00003960| 22 63 6f 6d 6d 61 6e 64 | 22 20 20 20 20 20 20 0a |"command|" .|
|00003970| 5c 65 6e 64 7b 76 65 72 | 62 61 74 69 6d 7d 20 0a |\end{ver|batim} .|
|00003980| 5c 6e 6f 70 61 67 65 62 | 72 65 61 6b 5c 68 72 75 |\nopageb|reak\hru|
|00003990| 6c 65 20 0a 5c 61 64 64 | 76 73 70 61 63 65 7b 5c |le .\add|vspace{\|
|000039a0| 6d 65 64 73 6b 69 70 61 | 6d 6f 75 6e 74 7d 0a 0a |medskipa|mount}..|
|000039b0| 0a 0a 0a 54 68 69 73 20 | 73 74 61 6e 64 61 72 64 |...This |standard|
|000039c0| 20 6c 69 62 72 61 72 79 | 20 66 75 6e 63 74 69 6f | library| functio|
|000039d0| 6e 20 63 72 65 61 74 65 | 73 20 61 20 68 61 6c 66 |n create|s a half|
|000039e0| 2d 64 75 70 6c 65 78 20 | 70 69 70 65 6c 69 6e 65 |-duplex |pipeline|
|000039f0| 20 62 79 20 63 61 6c 6c | 69 6e 67 0a 70 69 70 65 | by call|ing.pipe|
|00003a00| 28 29 20 69 6e 74 65 72 | 6e 61 6c 6c 79 2e 20 20 |() inter|nally. |
|00003a10| 49 74 20 74 68 65 6e 20 | 66 6f 72 6b 73 20 61 20 |It then |forks a |
|00003a20| 63 68 69 6c 64 20 70 72 | 6f 63 65 73 73 2c 20 65 |child pr|ocess, e|
|00003a30| 78 65 63 73 20 74 68 65 | 20 42 6f 75 72 6e 65 20 |xecs the| Bourne |
|00003a40| 73 68 65 6c 6c 2c 0a 61 | 6e 64 20 65 78 65 63 75 |shell,.a|nd execu|
|00003a50| 74 65 73 20 74 68 65 20 | 22 63 6f 6d 6d 61 6e 64 |tes the |"command|
|00003a60| 22 20 61 72 67 75 6d 65 | 6e 74 20 77 69 74 68 69 |" argume|nt withi|
|00003a70| 6e 20 74 68 65 20 73 68 | 65 6c 6c 2e 20 20 44 69 |n the sh|ell. Di|
|00003a80| 72 65 63 74 69 6f 6e 20 | 6f 66 20 64 61 74 61 0a |rection |of data.|
|00003a90| 66 6c 6f 77 20 69 73 20 | 64 65 74 65 72 6d 69 6e |flow is |determin|
|00003aa0| 65 64 20 62 79 20 74 68 | 65 20 73 65 63 6f 6e 64 |ed by th|e second|
|00003ab0| 20 61 72 67 75 6d 65 6e | 74 2c 20 22 74 79 70 65 | argumen|t, "type|
|00003ac0| 22 2e 20 20 49 74 20 63 | 61 6e 20 62 65 20 22 72 |". It c|an be "r|
|00003ad0| 22 20 6f 72 20 22 77 22 | 2c 20 66 6f 72 0a 22 72 |" or "w"|, for."r|
|00003ae0| 65 61 64 22 20 6f 72 20 | 22 77 72 69 74 65 22 2e |ead" or |"write".|
|00003af0| 20 20 49 74 20 63 61 6e | 6e 6f 74 20 62 65 20 62 | It can|not be b|
|00003b00| 6f 74 68 21 20 20 55 6e | 64 65 72 20 4c 69 6e 75 |oth! Un|der Linu|
|00003b10| 78 2c 20 74 68 65 20 70 | 69 70 65 20 77 69 6c 6c |x, the p|ipe will|
|00003b20| 20 62 65 20 6f 70 65 6e | 65 64 0a 75 70 20 69 6e | be open|ed.up in|
|00003b30| 20 74 68 65 20 6d 6f 64 | 65 20 73 70 65 63 69 66 | the mod|e specif|
|00003b40| 69 65 64 20 62 79 20 74 | 68 65 20 66 69 72 73 74 |ied by t|he first|
|00003b50| 20 63 68 61 72 61 63 74 | 65 72 20 6f 66 20 74 68 | charact|er of th|
|00003b60| 65 20 22 74 79 70 65 22 | 20 61 72 67 75 6d 65 6e |e "type"| argumen|
|00003b70| 74 2e 20 20 53 6f 2c 0a | 69 66 20 79 6f 75 20 74 |t. So,.|if you t|
|00003b80| 72 79 20 74 6f 20 70 61 | 73 73 20 22 72 77 22 2c |ry to pa|ss "rw",|
|00003b90| 20 69 74 20 77 69 6c 6c | 20 6f 6e 6c 79 20 6f 70 | it will| only op|
|00003ba0| 65 6e 20 69 74 20 75 70 | 20 69 6e 20 22 72 65 61 |en it up| in "rea|
|00003bb0| 64 22 20 6d 6f 64 65 2e | 0a 0a 57 68 69 6c 65 20 |d" mode.|..While |
|00003bc0| 74 68 69 73 20 6c 69 62 | 72 61 72 79 20 66 75 6e |this lib|rary fun|
|00003bd0| 63 74 69 6f 6e 20 70 65 | 72 66 6f 72 6d 73 20 71 |ction pe|rforms q|
|00003be0| 75 69 74 65 20 61 20 62 | 69 74 20 6f 66 20 74 68 |uite a b|it of th|
|00003bf0| 65 20 64 69 72 74 79 20 | 77 6f 72 6b 20 66 6f 72 |e dirty |work for|
|00003c00| 20 79 6f 75 2c 20 0a 74 | 68 65 72 65 20 69 73 20 | you, .t|here is |
|00003c10| 61 20 73 75 62 73 74 61 | 6e 74 69 61 6c 20 74 72 |a substa|ntial tr|
|00003c20| 61 64 65 6f 66 66 2e 20 | 20 59 6f 75 20 6c 6f 73 |adeoff. | You los|
|00003c30| 65 20 74 68 65 20 66 69 | 6e 65 20 63 6f 6e 74 72 |e the fi|ne contr|
|00003c40| 6f 6c 20 79 6f 75 20 6f | 6e 63 65 20 68 61 64 0a |ol you o|nce had.|
|00003c50| 62 79 20 75 73 69 6e 67 | 20 74 68 65 20 70 69 70 |by using| the pip|
|00003c60| 65 28 29 20 73 79 73 74 | 65 6d 20 63 61 6c 6c 2c |e() syst|em call,|
|00003c70| 20 61 6e 64 20 68 61 6e | 64 6c 69 6e 67 20 74 68 | and han|dling th|
|00003c80| 65 20 66 6f 72 6b 2f 65 | 78 65 63 20 79 6f 75 72 |e fork/e|xec your|
|00003c90| 73 65 6c 66 2e 20 20 48 | 6f 77 65 76 65 72 2c 0a |self. H|owever,.|
|00003ca0| 73 69 6e 63 65 20 74 68 | 65 20 42 6f 75 72 6e 65 |since th|e Bourne|
|00003cb0| 20 73 68 65 6c 6c 20 69 | 73 20 75 73 65 64 20 64 | shell i|s used d|
|00003cc0| 69 72 65 63 74 6c 79 2c | 20 73 68 65 6c 6c 20 6d |irectly,| shell m|
|00003cd0| 65 74 61 63 68 61 72 61 | 63 74 65 72 20 65 78 70 |etachara|cter exp|
|00003ce0| 61 6e 73 69 6f 6e 0a 28 | 69 6e 63 6c 75 64 69 6e |ansion.(|includin|
|00003cf0| 67 20 77 69 6c 64 63 61 | 72 64 73 29 20 69 73 20 |g wildca|rds) is |
|00003d00| 70 65 72 6d 69 73 73 69 | 62 6c 65 20 77 69 74 68 |permissi|ble with|
|00003d10| 69 6e 20 74 68 65 20 22 | 63 6f 6d 6d 61 6e 64 22 |in the "|command"|
|00003d20| 20 61 72 67 75 6d 65 6e | 74 2e 0a 0a 50 69 70 65 | argumen|t...Pipe|
|00003d30| 73 20 77 68 69 63 68 20 | 61 72 65 20 63 72 65 61 |s which |are crea|
|00003d40| 74 65 64 20 77 69 74 68 | 20 70 6f 70 65 6e 28 29 |ted with| popen()|
|00003d50| 20 6d 75 73 74 20 62 65 | 20 63 6c 6f 73 65 64 20 | must be| closed |
|00003d60| 77 69 74 68 20 70 63 6c | 6f 73 65 28 29 2e 20 20 |with pcl|ose(). |
|00003d70| 42 79 20 6e 6f 77 2c 0a | 79 6f 75 20 68 61 76 65 |By now,.|you have|
|00003d80| 20 70 72 6f 62 61 62 6c | 79 20 72 65 61 6c 69 7a | probabl|y realiz|
|00003d90| 65 64 20 74 68 61 74 20 | 70 6f 70 65 6e 2f 70 63 |ed that |popen/pc|
|00003da0| 6c 6f 73 65 20 73 68 61 | 72 65 20 61 20 73 74 72 |lose sha|re a str|
|00003db0| 69 6b 69 6e 67 20 72 65 | 73 65 6d 62 6c 61 6e 63 |iking re|semblanc|
|00003dc0| 65 0a 74 6f 20 74 68 65 | 20 73 74 61 6e 64 61 72 |e.to the| standar|
|00003dd0| 64 20 66 69 6c 65 20 73 | 74 72 65 61 6d 20 49 2f |d file s|tream I/|
|00003de0| 4f 20 66 75 6e 63 74 69 | 6f 6e 73 20 66 6f 70 65 |O functi|ons fope|
|00003df0| 6e 28 29 20 61 6e 64 20 | 66 63 6c 6f 73 65 28 29 |n() and |fclose()|
|00003e00| 2e 0a 0a 0a 0a 5c 70 61 | 72 0a 5c 61 64 64 76 73 |.....\pa|r.\addvs|
|00003e10| 70 61 63 65 7b 5c 6d 65 | 64 73 6b 69 70 61 6d 6f |pace{\me|dskipamo|
|00003e20| 75 6e 74 7d 0a 5c 6e 6f | 70 61 67 65 62 72 65 61 |unt}.\no|pagebrea|
|00003e30| 6b 5c 68 72 75 6c 65 0a | 5c 62 65 67 69 6e 7b 76 |k\hrule.|\begin{v|
|00003e40| 65 72 62 61 74 69 6d 7d | 0a 20 20 4c 49 42 52 41 |erbatim}|. LIBRA|
|00003e50| 52 59 20 46 55 4e 43 54 | 49 4f 4e 3a 20 70 63 6c |RY FUNCT|ION: pcl|
|00003e60| 6f 73 65 28 29 3b 20 20 | 20 20 20 20 20 20 20 20 |ose(); | |
|00003e70| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00003e80| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00003e90| 20 20 20 20 20 20 20 20 | 20 0a 0a 20 20 50 52 4f | | .. PRO|
|00003ea0| 54 4f 54 59 50 45 3a 20 | 69 6e 74 20 70 63 6c 6f |TOTYPE: |int pclo|
|00003eb0| 73 65 28 20 46 49 4c 45 | 20 2a 73 74 72 65 61 6d |se( FILE| *stream|
|00003ec0| 20 29 3b 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | ); | |
|00003ed0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00003ee0| 20 20 20 20 20 20 20 20 | 20 20 20 0a 20 20 20 20 | | . |
|00003ef0| 52 45 54 55 52 4e 53 3a | 20 65 78 69 74 20 73 74 |RETURNS:| exit st|
|00003f00| 61 74 75 73 20 6f 66 20 | 77 61 69 74 34 28 29 20 |atus of |wait4() |
|00003f10| 63 61 6c 6c 20 20 20 20 | 20 20 20 20 20 20 20 20 |call | |
|00003f20| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00003f30| 20 20 20 20 20 20 20 20 | 20 20 20 20 0a 20 20 20 | | . |
|00003f40| 20 20 20 20 20 20 20 20 | 20 20 2d 31 20 69 66 20 | | -1 if |
|00003f50| 22 73 74 72 65 61 6d 22 | 20 69 73 20 6e 6f 74 20 |"stream"| is not |
|00003f60| 76 61 6c 69 64 2c 20 6f | 72 20 69 66 20 77 61 69 |valid, o|r if wai|
|00003f70| 74 34 28 29 20 66 61 69 | 6c 73 20 20 20 20 20 20 |t4() fai|ls |
|00003f80| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 0a 0a 20 | | .. |
|00003f90| 20 4e 4f 54 45 53 3a 20 | 77 61 69 74 73 20 6f 6e | NOTES: |waits on|
|00003fa0| 20 74 68 65 20 70 69 70 | 65 20 70 72 6f 63 65 73 | the pip|e proces|
|00003fb0| 73 20 74 6f 20 74 65 72 | 6d 69 6e 61 74 65 2c 20 |s to ter|minate, |
|00003fc0| 74 68 65 6e 20 63 6c 6f | 73 65 73 20 74 68 65 20 |then clo|ses the |
|00003fd0| 73 74 72 65 61 6d 2e 20 | 20 20 20 20 20 20 20 0a |stream. | .|
|00003fe0| 5c 65 6e 64 7b 76 65 72 | 62 61 74 69 6d 7d 20 0a |\end{ver|batim} .|
|00003ff0| 5c 6e 6f 70 61 67 65 62 | 72 65 61 6b 5c 68 72 75 |\nopageb|reak\hru|
|00004000| 6c 65 20 0a 5c 61 64 64 | 76 73 70 61 63 65 7b 5c |le .\add|vspace{\|
|00004010| 6d 65 64 73 6b 69 70 61 | 6d 6f 75 6e 74 7d 0a 0a |medskipa|mount}..|
|00004020| 0a 0a 0a 54 68 65 20 70 | 63 6c 6f 73 65 28 29 20 |...The p|close() |
|00004030| 66 75 6e 63 74 69 6f 6e | 20 70 65 72 66 6f 72 6d |function| perform|
|00004040| 73 20 61 20 77 61 69 74 | 34 28 29 20 6f 6e 20 74 |s a wait|4() on t|
|00004050| 68 65 20 70 72 6f 63 65 | 73 73 20 66 6f 72 6b 65 |he proce|ss forke|
|00004060| 64 20 62 79 20 70 6f 70 | 65 6e 28 29 2e 0a 57 68 |d by pop|en()..Wh|
|00004070| 65 6e 20 69 74 20 72 65 | 74 75 72 6e 73 2c 20 69 |en it re|turns, i|
|00004080| 74 20 64 65 73 74 72 6f | 79 73 20 74 68 65 20 70 |t destro|ys the p|
|00004090| 69 70 65 20 61 6e 64 20 | 74 68 65 20 66 69 6c 65 |ipe and |the file|
|000040a0| 20 73 74 72 65 61 6d 2e | 20 20 4f 6e 63 65 20 61 | stream.| Once a|
|000040b0| 67 61 69 6e 2c 20 69 74 | 20 69 73 0a 73 79 6e 6f |gain, it| is.syno|
|000040c0| 6e 79 6d 6f 75 73 20 77 | 69 74 68 20 74 68 65 20 |nymous w|ith the |
|000040d0| 66 63 6c 6f 73 65 28 29 | 20 66 75 6e 63 74 69 6f |fclose()| functio|
|000040e0| 6e 20 66 6f 72 20 6e 6f | 72 6d 61 6c 20 73 74 72 |n for no|rmal str|
|000040f0| 65 61 6d 2d 62 61 73 65 | 64 20 66 69 6c 65 20 49 |eam-base|d file I|
|00004100| 2f 4f 2e 0a 0a 43 6f 6e | 73 69 64 65 72 20 74 68 |/O...Con|sider th|
|00004110| 69 73 20 65 78 61 6d 70 | 6c 65 2c 20 77 68 69 63 |is examp|le, whic|
|00004120| 68 20 6f 70 65 6e 73 20 | 75 70 20 61 20 70 69 70 |h opens |up a pip|
|00004130| 65 20 74 6f 20 74 68 65 | 20 73 6f 72 74 20 63 6f |e to the| sort co|
|00004140| 6d 6d 61 6e 64 2c 20 61 | 6e 64 20 70 72 6f 63 65 |mmand, a|nd proce|
|00004150| 65 64 73 0a 74 6f 20 73 | 6f 72 74 20 61 6e 20 61 |eds.to s|ort an a|
|00004160| 72 72 61 79 20 6f 66 20 | 73 74 72 69 6e 67 73 3a |rray of |strings:|
|00004170| 0a 0a 5c 62 65 67 69 6e | 7b 76 65 72 62 61 74 69 |..\begin|{verbati|
|00004180| 6d 7d 0a 0a 2f 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |m}../***|********|
|00004190| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|000041a0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|000041b0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|000041c0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|000041d0| 2a 2a 0a 20 45 78 63 65 | 72 70 74 20 66 72 6f 6d |**. Exce|rpt from|
|000041e0| 20 22 4c 69 6e 75 78 20 | 50 72 6f 67 72 61 6d 6d | "Linux |Programm|
|000041f0| 65 72 27 73 20 47 75 69 | 64 65 20 2d 20 43 68 61 |er's Gui|de - Cha|
|00004200| 70 74 65 72 20 36 22 0a | 20 28 43 29 6f 70 79 72 |pter 6".| (C)opyr|
|00004210| 69 67 68 74 20 31 39 39 | 34 2d 31 39 39 35 2c 20 |ight 199|4-1995, |
|00004220| 53 63 6f 74 74 20 42 75 | 72 6b 65 74 74 0a 20 2a |Scott Bu|rkett. *|
|00004230| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00004240| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00004250| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00004260| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00004270| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 20 0a 20 4d |********|**** . M|
|00004280| 4f 44 55 4c 45 3a 20 70 | 6f 70 65 6e 31 2e 63 0a |ODULE: p|open1.c.|
|00004290| 20 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a | *******|********|
|000042a0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|000042b0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|000042c0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|000042d0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2f 0a |********|******/.|
|000042e0| 20 20 0a 23 69 6e 63 6c | 75 64 65 20 3c 73 74 64 | .#incl|ude <std|
|000042f0| 69 6f 2e 68 3e 0a 0a 23 | 64 65 66 69 6e 65 20 4d |io.h>..#|define M|
|00004300| 41 58 53 54 52 53 20 35 | 0a 0a 69 6e 74 20 6d 61 |AXSTRS 5|..int ma|
|00004310| 69 6e 28 76 6f 69 64 29 | 0a 7b 0a 20 20 20 20 20 |in(void)|.{. |
|00004320| 20 20 20 69 6e 74 20 20 | 63 6e 74 72 3b 0a 20 20 | int |cntr;. |
|00004330| 20 20 20 20 20 20 46 49 | 4c 45 20 2a 70 69 70 65 | FI|LE *pipe|
|00004340| 5f 66 70 3b 0a 20 20 20 | 20 20 20 20 20 63 68 61 |_fp;. | cha|
|00004350| 72 20 2a 73 74 72 69 6e | 67 73 5b 4d 41 58 53 54 |r *strin|gs[MAXST|
|00004360| 52 53 5d 20 3d 20 7b 20 | 22 65 63 68 6f 22 2c 20 |RS] = { |"echo", |
|00004370| 22 62 72 61 76 6f 22 2c | 20 22 61 6c 70 68 61 22 |"bravo",| "alpha"|
|00004380| 2c 0a 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |,. | |
|00004390| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000043a0| 20 20 20 20 22 63 68 61 | 72 6c 69 65 22 2c 20 22 | "cha|rlie", "|
|000043b0| 64 65 6c 74 61 22 7d 3b | 0a 0a 20 20 20 20 20 20 |delta"};|.. |
|000043c0| 20 20 2f 2a 20 43 72 65 | 61 74 65 20 6f 6e 65 20 | /* Cre|ate one |
|000043d0| 77 61 79 20 70 69 70 65 | 20 6c 69 6e 65 20 77 69 |way pipe| line wi|
|000043e0| 74 68 20 63 61 6c 6c 20 | 74 6f 20 70 6f 70 65 6e |th call |to popen|
|000043f0| 28 29 20 2a 2f 0a 20 20 | 20 20 20 20 20 20 69 66 |() */. | if|
|00004400| 20 28 28 20 70 69 70 65 | 5f 66 70 20 3d 20 70 6f | (( pipe|_fp = po|
|00004410| 70 65 6e 28 22 73 6f 72 | 74 22 2c 20 22 77 22 29 |pen("sor|t", "w")|
|00004420| 29 20 3d 3d 20 4e 55 4c | 4c 29 0a 20 20 20 20 20 |) == NUL|L). |
|00004430| 20 20 20 7b 0a 20 20 20 | 20 20 20 20 20 20 20 20 | {. | |
|00004440| 20 20 20 20 20 70 65 72 | 72 6f 72 28 22 70 6f 70 | per|ror("pop|
|00004450| 65 6e 22 29 3b 0a 20 20 | 20 20 20 20 20 20 20 20 |en");. | |
|00004460| 20 20 20 20 20 20 65 78 | 69 74 28 31 29 3b 0a 20 | ex|it(1);. |
|00004470| 20 20 20 20 20 20 20 7d | 0a 0a 20 20 20 20 20 20 | }|.. |
|00004480| 20 20 2f 2a 20 50 72 6f | 63 65 73 73 69 6e 67 20 | /* Pro|cessing |
|00004490| 6c 6f 6f 70 20 2a 2f 0a | 20 20 20 20 20 20 20 20 |loop */.| |
|000044a0| 66 6f 72 28 63 6e 74 72 | 3d 30 3b 20 63 6e 74 72 |for(cntr|=0; cntr|
|000044b0| 3c 4d 41 58 53 54 52 53 | 3b 20 63 6e 74 72 2b 2b |<MAXSTRS|; cntr++|
|000044c0| 29 20 7b 0a 20 20 20 20 | 20 20 20 20 20 20 20 20 |) {. | |
|000044d0| 20 20 20 20 66 70 75 74 | 73 28 73 74 72 69 6e 67 | fput|s(string|
|000044e0| 73 5b 63 6e 74 72 5d 2c | 20 70 69 70 65 5f 66 70 |s[cntr],| pipe_fp|
|000044f0| 29 3b 0a 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |);. | |
|00004500| 20 20 20 66 70 75 74 63 | 28 27 5c 6e 27 2c 20 70 | fputc|('\n', p|
|00004510| 69 70 65 5f 66 70 29 3b | 0a 20 20 20 20 20 20 20 |ipe_fp);|. |
|00004520| 20 7d 0a 0a 20 20 20 20 | 20 20 20 20 2f 2a 20 43 | }.. | /* C|
|00004530| 6c 6f 73 65 20 74 68 65 | 20 70 69 70 65 20 2a 2f |lose the| pipe */|
|00004540| 0a 20 20 20 20 20 20 20 | 20 70 63 6c 6f 73 65 28 |. | pclose(|
|00004550| 70 69 70 65 5f 66 70 29 | 3b 0a 20 20 20 20 20 20 |pipe_fp)|;. |
|00004560| 20 20 0a 20 20 20 20 20 | 20 20 20 72 65 74 75 72 | . | retur|
|00004570| 6e 28 30 29 3b 0a 7d 0a | 5c 65 6e 64 7b 76 65 72 |n(0);.}.|\end{ver|
|00004580| 62 61 74 69 6d 7d 0a 0a | 0a 0a 0a 53 69 6e 63 65 |batim}..|...Since|
|00004590| 20 7b 5c 74 74 20 70 6f | 70 65 6e 28 29 7d 20 75 | {\tt po|pen()} u|
|000045a0| 73 65 73 20 74 68 65 20 | 73 68 65 6c 6c 20 74 6f |ses the |shell to|
|000045b0| 20 64 6f 20 69 74 73 20 | 62 69 64 64 69 6e 67 2c | do its |bidding,|
|000045c0| 20 61 6c 6c 20 73 68 65 | 6c 6c 20 65 78 70 61 6e | all she|ll expan|
|000045d0| 73 69 6f 6e 0a 63 68 61 | 72 61 63 74 65 72 73 20 |sion.cha|racters |
|000045e0| 61 6e 64 20 6d 65 74 61 | 63 68 61 72 61 63 74 65 |and meta|characte|
|000045f0| 72 73 20 61 72 65 20 61 | 76 61 69 6c 61 62 6c 65 |rs are a|vailable|
|00004600| 20 66 6f 72 20 75 73 65 | 21 20 20 49 6e 20 61 64 | for use|! In ad|
|00004610| 64 69 74 69 6f 6e 2c 20 | 6d 6f 72 65 20 20 0a 61 |dition, |more .a|
|00004620| 64 76 61 6e 63 65 64 20 | 74 65 63 68 6e 69 71 75 |dvanced |techniqu|
|00004630| 65 73 20 73 75 63 68 20 | 61 73 20 72 65 64 69 72 |es such |as redir|
|00004640| 65 63 74 69 6f 6e 2c 20 | 61 6e 64 20 65 76 65 6e |ection, |and even|
|00004650| 20 6f 75 74 70 75 74 20 | 70 69 70 69 6e 67 2c 20 | output |piping, |
|00004660| 63 61 6e 20 62 65 20 75 | 74 69 6c 69 7a 65 64 0a |can be u|tilized.|
|00004670| 77 69 74 68 20 7b 5c 74 | 74 20 70 6f 70 65 6e 28 |with {\t|t popen(|
|00004680| 29 7d 2e 20 20 43 6f 6e | 73 69 64 65 72 20 74 68 |)}. Con|sider th|
|00004690| 65 20 66 6f 6c 6c 6f 77 | 69 6e 67 20 73 61 6d 70 |e follow|ing samp|
|000046a0| 6c 65 20 63 61 6c 6c 73 | 3a 0a 0a 5c 62 65 67 69 |le calls|:..\begi|
|000046b0| 6e 7b 76 65 72 62 61 74 | 69 6d 7d 0a 20 20 20 20 |n{verbat|im}. |
|000046c0| 20 20 20 20 70 6f 70 65 | 6e 28 22 6c 73 20 7e 73 | pope|n("ls ~s|
|000046d0| 63 6f 74 74 62 22 2c 20 | 22 72 22 29 3b 0a 20 20 |cottb", |"r");. |
|000046e0| 20 20 20 20 20 20 70 6f | 70 65 6e 28 22 73 6f 72 | po|pen("sor|
|000046f0| 74 20 3e 20 2f 74 6d 70 | 2f 66 6f 6f 22 2c 20 22 |t > /tmp|/foo", "|
|00004700| 77 22 29 3b 0a 20 20 20 | 20 20 20 20 20 70 6f 70 |w");. | pop|
|00004710| 65 6e 28 22 73 6f 72 74 | 20 7c 20 75 6e 69 71 20 |en("sort| | uniq |
|00004720| 7c 20 6d 6f 72 65 22 2c | 20 22 77 22 29 3b 0a 5c || more",| "w");.\|
|00004730| 65 6e 64 7b 76 65 72 62 | 61 74 69 6d 7d 0a 0a 0a |end{verb|atim}...|
|00004740| 0a 0a 41 73 20 61 6e 6f | 74 68 65 72 20 65 78 61 |..As ano|ther exa|
|00004750| 6d 70 6c 65 20 6f 66 20 | 70 6f 70 65 6e 28 29 2c |mple of |popen(),|
|00004760| 20 63 6f 6e 73 69 64 65 | 72 20 74 68 69 73 20 73 | conside|r this s|
|00004770| 6d 61 6c 6c 20 70 72 6f | 67 72 61 6d 2c 20 77 68 |mall pro|gram, wh|
|00004780| 69 63 68 20 6f 70 65 6e | 73 0a 75 70 20 74 77 6f |ich open|s.up two|
|00004790| 20 70 69 70 65 73 20 28 | 6f 6e 65 20 74 6f 20 74 | pipes (|one to t|
|000047a0| 68 65 20 6c 73 20 63 6f | 6d 6d 61 6e 64 2c 20 74 |he ls co|mmand, t|
|000047b0| 68 65 20 6f 74 68 65 72 | 20 74 6f 20 73 6f 72 74 |he other| to sort|
|000047c0| 29 3a 0a 0a 5c 62 65 67 | 69 6e 7b 76 65 72 62 61 |):..\beg|in{verba|
|000047d0| 74 69 6d 7d 0a 0a 2f 2a | 2a 2a 2a 2a 2a 2a 2a 2a |tim}../*|********|
|000047e0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|000047f0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00004800| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00004810| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00004820| 2a 2a 2a 2a 0a 20 45 78 | 63 65 72 70 74 20 66 72 |****. Ex|cerpt fr|
|00004830| 6f 6d 20 22 4c 69 6e 75 | 78 20 50 72 6f 67 72 61 |om "Linu|x Progra|
|00004840| 6d 6d 65 72 27 73 20 47 | 75 69 64 65 20 2d 20 43 |mmer's G|uide - C|
|00004850| 68 61 70 74 65 72 20 36 | 22 0a 20 28 43 29 6f 70 |hapter 6|". (C)op|
|00004860| 79 72 69 67 68 74 20 31 | 39 39 34 2d 31 39 39 35 |yright 1|994-1995|
|00004870| 2c 20 53 63 6f 74 74 20 | 42 75 72 6b 65 74 74 0a |, Scott |Burkett.|
|00004880| 20 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a | *******|********|
|00004890| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|000048a0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|000048b0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|000048c0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 20 0a |********|****** .|
|000048d0| 20 4d 4f 44 55 4c 45 3a | 20 70 6f 70 65 6e 32 2e | MODULE:| popen2.|
|000048e0| 63 0a 20 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |c. *****|********|
|000048f0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00004900| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00004910| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00004920| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00004930| 2f 0a 0a 23 69 6e 63 6c | 75 64 65 20 3c 73 74 64 |/..#incl|ude <std|
|00004940| 69 6f 2e 68 3e 0a 0a 69 | 6e 74 20 6d 61 69 6e 28 |io.h>..i|nt main(|
|00004950| 76 6f 69 64 29 0a 7b 0a | 20 20 20 20 20 20 20 20 |void).{.| |
|00004960| 46 49 4c 45 20 2a 70 69 | 70 65 69 6e 5f 66 70 2c |FILE *pi|pein_fp,|
|00004970| 20 2a 70 69 70 65 6f 75 | 74 5f 66 70 3b 0a 20 20 | *pipeou|t_fp;. |
|00004980| 20 20 20 20 20 20 63 68 | 61 72 20 72 65 61 64 62 | ch|ar readb|
|00004990| 75 66 5b 38 30 5d 3b 0a | 0a 20 20 20 20 20 20 20 |uf[80];.|. |
|000049a0| 20 2f 2a 20 43 72 65 61 | 74 65 20 6f 6e 65 20 77 | /* Crea|te one w|
|000049b0| 61 79 20 70 69 70 65 20 | 6c 69 6e 65 20 77 69 74 |ay pipe |line wit|
|000049c0| 68 20 63 61 6c 6c 20 74 | 6f 20 70 6f 70 65 6e 28 |h call t|o popen(|
|000049d0| 29 20 2a 2f 0a 20 20 20 | 20 20 20 20 20 69 66 20 |) */. | if |
|000049e0| 28 28 20 70 69 70 65 69 | 6e 5f 66 70 20 3d 20 70 |(( pipei|n_fp = p|
|000049f0| 6f 70 65 6e 28 22 6c 73 | 22 2c 20 22 72 22 29 29 |open("ls|", "r"))|
|00004a00| 20 3d 3d 20 4e 55 4c 4c | 29 0a 20 20 20 20 20 20 | == NULL|). |
|00004a10| 20 20 7b 0a 20 20 20 20 | 20 20 20 20 20 20 20 20 | {. | |
|00004a20| 20 20 20 20 70 65 72 72 | 6f 72 28 22 70 6f 70 65 | perr|or("pope|
|00004a30| 6e 22 29 3b 0a 20 20 20 | 20 20 20 20 20 20 20 20 |n");. | |
|00004a40| 20 20 20 20 20 65 78 69 | 74 28 31 29 3b 0a 20 20 | exi|t(1);. |
|00004a50| 20 20 20 20 20 20 7d 0a | 0a 20 20 20 20 20 20 20 | }.|. |
|00004a60| 20 2f 2a 20 43 72 65 61 | 74 65 20 6f 6e 65 20 77 | /* Crea|te one w|
|00004a70| 61 79 20 70 69 70 65 20 | 6c 69 6e 65 20 77 69 74 |ay pipe |line wit|
|00004a80| 68 20 63 61 6c 6c 20 74 | 6f 20 70 6f 70 65 6e 28 |h call t|o popen(|
|00004a90| 29 20 2a 2f 0a 20 20 20 | 20 20 20 20 20 69 66 20 |) */. | if |
|00004aa0| 28 28 20 70 69 70 65 6f | 75 74 5f 66 70 20 3d 20 |(( pipeo|ut_fp = |
|00004ab0| 70 6f 70 65 6e 28 22 73 | 6f 72 74 22 2c 20 22 77 |popen("s|ort", "w|
|00004ac0| 22 29 29 20 3d 3d 20 4e | 55 4c 4c 29 0a 20 20 20 |")) == N|ULL). |
|00004ad0| 20 20 20 20 20 7b 0a 20 | 20 20 20 20 20 20 20 20 | {. | |
|00004ae0| 20 20 20 20 20 20 20 70 | 65 72 72 6f 72 28 22 70 | p|error("p|
|00004af0| 6f 70 65 6e 22 29 3b 0a | 20 20 20 20 20 20 20 20 |open");.| |
|00004b00| 20 20 20 20 20 20 20 20 | 65 78 69 74 28 31 29 3b | |exit(1);|
|00004b10| 0a 20 20 20 20 20 20 20 | 20 7d 0a 0a 20 20 20 20 |. | }.. |
|00004b20| 20 20 20 20 2f 2a 20 50 | 72 6f 63 65 73 73 69 6e | /* P|rocessin|
|00004b30| 67 20 6c 6f 6f 70 20 2a | 2f 0a 20 20 20 20 20 20 |g loop *|/. |
|00004b40| 20 20 77 68 69 6c 65 28 | 66 67 65 74 73 28 72 65 | while(|fgets(re|
|00004b50| 61 64 62 75 66 2c 20 38 | 30 2c 20 70 69 70 65 69 |adbuf, 8|0, pipei|
|00004b60| 6e 5f 66 70 29 29 0a 20 | 20 20 20 20 20 20 20 20 |n_fp)). | |
|00004b70| 20 20 20 20 20 20 20 66 | 70 75 74 73 28 72 65 61 | f|puts(rea|
|00004b80| 64 62 75 66 2c 20 70 69 | 70 65 6f 75 74 5f 66 70 |dbuf, pi|peout_fp|
|00004b90| 29 3b 0a 0a 20 20 20 20 | 20 20 20 20 2f 2a 20 43 |);.. | /* C|
|00004ba0| 6c 6f 73 65 20 74 68 65 | 20 70 69 70 65 73 20 2a |lose the| pipes *|
|00004bb0| 2f 0a 20 20 20 20 20 20 | 20 20 70 63 6c 6f 73 65 |/. | pclose|
|00004bc0| 28 70 69 70 65 69 6e 5f | 66 70 29 3b 0a 20 20 20 |(pipein_|fp);. |
|00004bd0| 20 20 20 20 20 70 63 6c | 6f 73 65 28 70 69 70 65 | pcl|ose(pipe|
|00004be0| 6f 75 74 5f 66 70 29 3b | 0a 0a 20 20 20 20 20 20 |out_fp);|.. |
|00004bf0| 20 20 72 65 74 75 72 6e | 28 30 29 3b 0a 7d 0a 5c | return|(0);.}.\|
|00004c00| 65 6e 64 7b 76 65 72 62 | 61 74 69 6d 7d 0a 0a 0a |end{verb|atim}...|
|00004c10| 0a 0a 46 6f 72 20 6f 75 | 72 20 66 69 6e 61 6c 20 |..For ou|r final |
|00004c20| 64 65 6d 6f 6e 73 74 72 | 61 74 69 6f 6e 20 6f 66 |demonstr|ation of|
|00004c30| 20 70 6f 70 65 6e 28 29 | 2c 20 6c 65 74 27 73 20 | popen()|, let's |
|00004c40| 63 72 65 61 74 65 20 61 | 20 67 65 6e 65 72 69 63 |create a| generic|
|00004c50| 20 70 72 6f 67 72 61 6d | 20 74 68 61 74 0a 6f 70 | program| that.op|
|00004c60| 65 6e 73 20 75 70 20 61 | 20 70 69 70 65 6c 69 6e |ens up a| pipelin|
|00004c70| 65 20 62 65 74 77 65 65 | 6e 20 61 20 70 61 73 73 |e betwee|n a pass|
|00004c80| 65 64 20 63 6f 6d 6d 61 | 6e 64 20 61 6e 64 20 66 |ed comma|nd and f|
|00004c90| 69 6c 65 6e 61 6d 65 3a | 0a 0a 5c 62 65 67 69 6e |ilename:|..\begin|
|00004ca0| 7b 76 65 72 62 61 74 69 | 6d 7d 0a 0a 2f 2a 2a 2a |{verbati|m}../***|
|00004cb0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00004cc0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00004cd0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00004ce0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00004cf0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 0a 20 45 78 63 65 |********|**. Exce|
|00004d00| 72 70 74 20 66 72 6f 6d | 20 22 4c 69 6e 75 78 20 |rpt from| "Linux |
|00004d10| 50 72 6f 67 72 61 6d 6d | 65 72 27 73 20 47 75 69 |Programm|er's Gui|
|00004d20| 64 65 20 2d 20 43 68 61 | 70 74 65 72 20 36 22 0a |de - Cha|pter 6".|
|00004d30| 20 28 43 29 6f 70 79 72 | 69 67 68 74 20 31 39 39 | (C)opyr|ight 199|
|00004d40| 34 2d 31 39 39 35 2c 20 | 53 63 6f 74 74 20 42 75 |4-1995, |Scott Bu|
|00004d50| 72 6b 65 74 74 0a 20 2a | 2a 2a 2a 2a 2a 2a 2a 2a |rkett. *|********|
|00004d60| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00004d70| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00004d80| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00004d90| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00004da0| 2a 2a 2a 2a 20 0a 20 4d | 4f 44 55 4c 45 3a 20 70 |**** . M|ODULE: p|
|00004db0| 6f 70 65 6e 33 2e 63 0a | 20 2a 2a 2a 2a 2a 2a 2a |open3.c.| *******|
|00004dc0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00004dd0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00004de0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00004df0| 2a 2a 2a 2a 2a 2a 2a 2a | 2a 2a 2a 2a 2a 2a 2a 2a |********|********|
|00004e00| 2a 2a 2a 2a 2a 2a 2f 0a | 0a 23 69 6e 63 6c 75 64 |******/.|.#includ|
|00004e10| 65 20 3c 73 74 64 69 6f | 2e 68 3e 0a 0a 69 6e 74 |e <stdio|.h>..int|
|00004e20| 20 6d 61 69 6e 28 69 6e | 74 20 61 72 67 63 2c 20 | main(in|t argc, |
|00004e30| 63 68 61 72 20 2a 61 72 | 67 76 5b 5d 29 0a 7b 0a |char *ar|gv[]).{.|
|00004e40| 20 20 20 20 20 20 20 20 | 46 49 4c 45 20 2a 70 69 | |FILE *pi|
|00004e50| 70 65 5f 66 70 2c 20 2a | 69 6e 66 69 6c 65 3b 0a |pe_fp, *|infile;.|
|00004e60| 20 20 20 20 20 20 20 20 | 63 68 61 72 20 72 65 61 | |char rea|
|00004e70| 64 62 75 66 5b 38 30 5d | 3b 0a 0a 20 20 20 20 20 |dbuf[80]|;.. |
|00004e80| 20 20 20 69 66 28 20 61 | 72 67 63 20 21 3d 20 33 | if( a|rgc != 3|
|00004e90| 29 20 7b 0a 20 20 20 20 | 20 20 20 20 20 20 20 20 |) {. | |
|00004ea0| 20 20 20 20 66 70 72 69 | 6e 74 66 28 73 74 64 65 | fpri|ntf(stde|
|00004eb0| 72 72 2c 20 22 55 53 41 | 47 45 3a 20 20 70 6f 70 |rr, "USA|GE: pop|
|00004ec0| 65 6e 33 20 5b 63 6f 6d | 6d 61 6e 64 5d 20 5b 66 |en3 [com|mand] [f|
|00004ed0| 69 6c 65 6e 61 6d 65 5d | 5c 6e 22 29 3b 20 20 20 |ilename]|\n"); |
|00004ee0| 20 20 20 20 0a 20 20 20 | 20 20 20 20 20 20 20 20 | . | |
|00004ef0| 20 20 20 20 20 65 78 69 | 74 28 31 29 3b 0a 20 20 | exi|t(1);. |
|00004f00| 20 20 20 20 20 20 7d 0a | 0a 20 20 20 20 20 20 20 | }.|. |
|00004f10| 20 2f 2a 20 4f 70 65 6e | 20 75 70 20 69 6e 70 75 | /* Open| up inpu|
|00004f20| 74 20 66 69 6c 65 20 2a | 2f 0a 20 20 20 20 20 20 |t file *|/. |
|00004f30| 20 20 69 66 20 28 28 20 | 69 6e 66 69 6c 65 20 3d | if (( |infile =|
|00004f40| 20 66 6f 70 65 6e 28 61 | 72 67 76 5b 32 5d 2c 20 | fopen(a|rgv[2], |
|00004f50| 22 72 74 22 29 29 20 3d | 3d 20 4e 55 4c 4c 29 0a |"rt")) =|= NULL).|
|00004f60| 20 20 20 20 20 20 20 20 | 7b 0a 20 20 20 20 20 20 | |{. |
|00004f70| 20 20 20 20 20 20 20 20 | 20 20 70 65 72 72 6f 72 | | perror|
|00004f80| 28 22 66 6f 70 65 6e 22 | 29 3b 0a 20 20 20 20 20 |("fopen"|);. |
|00004f90| 20 20 20 20 20 20 20 20 | 20 20 20 65 78 69 74 28 | | exit(|
|00004fa0| 31 29 3b 20 20 20 20 20 | 20 20 20 0a 20 20 20 20 |1); | . |
|00004fb0| 20 20 20 20 7d 0a 0a 20 | 20 20 20 20 20 20 20 2f | }.. | /|
|00004fc0| 2a 20 43 72 65 61 74 65 | 20 6f 6e 65 20 77 61 79 |* Create| one way|
|00004fd0| 20 70 69 70 65 20 6c 69 | 6e 65 20 77 69 74 68 20 | pipe li|ne with |
|00004fe0| 63 61 6c 6c 20 74 6f 20 | 70 6f 70 65 6e 28 29 20 |call to |popen() |
|00004ff0| 2a 2f 0a 20 20 20 20 20 | 20 20 20 69 66 20 28 28 |*/. | if ((|
|00005000| 20 70 69 70 65 5f 66 70 | 20 3d 20 70 6f 70 65 6e | pipe_fp| = popen|
|00005010| 28 61 72 67 76 5b 31 5d | 2c 20 22 77 22 29 29 20 |(argv[1]|, "w")) |
|00005020| 3d 3d 20 4e 55 4c 4c 29 | 0a 20 20 20 20 20 20 20 |== NULL)|. |
|00005030| 20 7b 0a 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | {. | |
|00005040| 20 20 20 70 65 72 72 6f | 72 28 22 70 6f 70 65 6e | perro|r("popen|
|00005050| 22 29 3b 0a 20 20 20 20 | 20 20 20 20 20 20 20 20 |");. | |
|00005060| 20 20 20 20 65 78 69 74 | 28 31 29 3b 0a 20 20 20 | exit|(1);. |
|00005070| 20 20 20 20 20 7d 0a 0a | 20 20 20 20 20 20 20 20 | }..| |
|00005080| 2f 2a 20 50 72 6f 63 65 | 73 73 69 6e 67 20 6c 6f |/* Proce|ssing lo|
|00005090| 6f 70 20 2a 2f 0a 20 20 | 20 20 20 20 20 20 64 6f |op */. | do|
|000050a0| 20 7b 20 0a 20 20 20 20 | 20 20 20 20 20 20 20 20 | { . | |
|000050b0| 20 20 20 20 66 67 65 74 | 73 28 72 65 61 64 62 75 | fget|s(readbu|
|000050c0| 66 2c 20 38 30 2c 20 69 | 6e 66 69 6c 65 29 3b 0a |f, 80, i|nfile);.|
|000050d0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000050e0| 69 66 28 66 65 6f 66 28 | 69 6e 66 69 6c 65 29 29 |if(feof(|infile))|
|000050f0| 20 62 72 65 61 6b 3b 0a | 0a 20 20 20 20 20 20 20 | break;.|. |
|00005100| 20 20 20 20 20 20 20 20 | 20 66 70 75 74 73 28 72 | | fputs(r|
|00005110| 65 61 64 62 75 66 2c 20 | 70 69 70 65 5f 66 70 29 |eadbuf, |pipe_fp)|
|00005120| 3b 0a 20 20 20 20 20 20 | 20 20 7d 20 77 68 69 6c |;. | } whil|
|00005130| 65 28 21 66 65 6f 66 28 | 69 6e 66 69 6c 65 29 29 |e(!feof(|infile))|
|00005140| 3b 0a 0a 20 20 20 20 20 | 20 20 20 66 63 6c 6f 73 |;.. | fclos|
|00005150| 65 28 69 6e 66 69 6c 65 | 29 3b 20 0a 20 20 20 20 |e(infile|); . |
|00005160| 20 20 20 20 70 63 6c 6f | 73 65 28 70 69 70 65 5f | pclo|se(pipe_|
|00005170| 66 70 29 3b 0a 0a 20 20 | 20 20 20 20 20 20 72 65 |fp);.. | re|
|00005180| 74 75 72 6e 28 30 29 3b | 0a 7d 0a 5c 65 6e 64 7b |turn(0);|.}.\end{|
|00005190| 76 65 72 62 61 74 69 6d | 7d 0a 0a 0a 0a 0a 54 72 |verbatim|}.....Tr|
|000051a0| 79 20 74 68 69 73 20 70 | 72 6f 67 72 61 6d 20 6f |y this p|rogram o|
|000051b0| 75 74 2c 20 77 69 74 68 | 20 74 68 65 20 66 6f 6c |ut, with| the fol|
|000051c0| 6c 6f 77 69 6e 67 20 69 | 6e 76 6f 63 61 74 69 6f |lowing i|nvocatio|
|000051d0| 6e 73 3a 0a 5c 62 65 67 | 69 6e 7b 76 65 72 62 61 |ns:.\beg|in{verba|
|000051e0| 74 69 6d 7d 0a 20 20 20 | 20 20 20 20 20 70 6f 70 |tim}. | pop|
|000051f0| 65 6e 33 20 73 6f 72 74 | 20 70 6f 70 65 6e 33 2e |en3 sort| popen3.|
|00005200| 63 0a 20 20 20 20 20 20 | 20 20 70 6f 70 65 6e 33 |c. | popen3|
|00005210| 20 63 61 74 20 70 6f 70 | 65 6e 33 2e 63 0a 20 20 | cat pop|en3.c. |
|00005220| 20 20 20 20 20 20 70 6f | 70 65 6e 33 20 6d 6f 72 | po|pen3 mor|
|00005230| 65 20 70 6f 70 65 6e 33 | 2e 63 0a 20 20 20 20 20 |e popen3|.c. |
|00005240| 20 20 20 70 6f 70 65 6e | 33 20 63 61 74 20 70 6f | popen|3 cat po|
|00005250| 70 65 6e 33 2e 63 20 7c | 20 67 72 65 70 20 6d 61 |pen3.c || grep ma|
|00005260| 69 6e 0a 5c 65 6e 64 7b | 76 65 72 62 61 74 69 6d |in.\end{|verbatim|
|00005270| 7d 0a 0a 0a 0a 0a 0a 0a | 0a 5c 73 75 62 73 65 63 |}.......|.\subsec|
|00005280| 74 69 6f 6e 7b 41 74 6f | 6d 69 63 20 4f 70 65 72 |tion{Ato|mic Oper|
|00005290| 61 74 69 6f 6e 73 20 77 | 69 74 68 20 50 69 70 65 |ations w|ith Pipe|
|000052a0| 73 7d 0a 0a 0a 0a 49 6e | 20 6f 72 64 65 72 20 66 |s}....In| order f|
|000052b0| 6f 72 20 61 6e 20 6f 70 | 65 72 61 74 69 6f 6e 20 |or an op|eration |
|000052c0| 74 6f 20 62 65 20 63 6f | 6e 73 69 64 65 72 65 64 |to be co|nsidered|
|000052d0| 20 60 60 61 74 6f 6d 69 | 63 27 27 2c 20 69 74 20 | ``atomi|c'', it |
|000052e0| 6d 75 73 74 20 6e 6f 74 | 20 62 65 20 69 6e 74 65 |must not| be inte|
|000052f0| 72 72 75 70 74 65 64 0a | 66 6f 72 20 61 6e 79 20 |rrupted.|for any |
|00005300| 72 65 61 73 6f 6e 20 61 | 74 20 61 6c 6c 2e 20 20 |reason a|t all. |
|00005310| 54 68 65 20 65 6e 74 69 | 72 65 20 6f 70 65 72 61 |The enti|re opera|
|00005320| 74 69 6f 6e 20 6f 63 63 | 75 72 73 20 61 74 20 6f |tion occ|urs at o|
|00005330| 6e 63 65 2e 20 20 54 68 | 65 20 50 4f 53 49 58 20 |nce. Th|e POSIX |
|00005340| 0a 73 74 61 6e 64 61 72 | 64 20 64 69 63 74 61 74 |.standar|d dictat|
|00005350| 65 73 20 69 6e 20 2f 75 | 73 72 2f 69 6e 63 6c 75 |es in /u|sr/inclu|
|00005360| 64 65 2f 70 6f 73 69 78 | 31 5c 5f 6c 69 6d 2e 68 |de/posix|1\_lim.h|
|00005370| 20 74 68 61 74 20 74 68 | 65 20 6d 61 78 69 6d 75 | that th|e maximu|
|00005380| 6d 20 62 75 66 66 65 72 | 20 73 69 7a 65 0a 66 6f |m buffer| size.fo|
|00005390| 72 20 61 6e 20 61 74 6f | 6d 69 63 20 6f 70 65 72 |r an ato|mic oper|
|000053a0| 61 74 69 6f 6e 20 6f 6e | 20 61 20 70 69 70 65 20 |ation on| a pipe |
|000053b0| 69 73 3a 0a 0a 5c 62 65 | 67 69 6e 7b 76 65 72 62 |is:..\be|gin{verb|
|000053c0| 61 74 69 6d 7d 0a 20 20 | 20 20 20 20 20 20 23 64 |atim}. | #d|
|000053d0| 65 66 69 6e 65 20 5f 50 | 4f 53 49 58 5f 50 49 50 |efine _P|OSIX_PIP|
|000053e0| 45 5f 42 55 46 20 20 20 | 20 20 20 20 20 20 35 31 |E_BUF | 51|
|000053f0| 32 0a 5c 65 6e 64 7b 76 | 65 72 62 61 74 69 6d 7d |2.\end{v|erbatim}|
|00005400| 0a 0a 0a 0a 0a 55 70 20 | 74 6f 20 35 31 32 20 62 |.....Up |to 512 b|
|00005410| 79 74 65 73 20 63 61 6e | 20 62 65 20 77 72 69 74 |ytes can| be writ|
|00005420| 74 65 6e 20 6f 72 20 72 | 65 74 72 69 65 76 65 64 |ten or r|etrieved|
|00005430| 20 66 72 6f 6d 20 61 20 | 70 69 70 65 20 61 74 6f | from a |pipe ato|
|00005440| 6d 69 63 61 6c 6c 79 2e | 20 20 41 6e 79 74 68 69 |mically.| Anythi|
|00005450| 6e 67 0a 74 68 61 74 20 | 63 72 6f 73 73 65 73 20 |ng.that |crosses |
|00005460| 74 68 69 73 20 74 68 72 | 65 73 68 6f 6c 64 20 77 |this thr|eshold w|
|00005470| 69 6c 6c 20 62 65 20 73 | 70 6c 69 74 2c 20 61 6e |ill be s|plit, an|
|00005480| 64 20 6e 6f 74 20 61 74 | 6f 6d 69 63 2e 20 20 55 |d not at|omic. U|
|00005490| 6e 64 65 72 20 4c 69 6e | 75 78 2c 20 20 0a 68 6f |nder Lin|ux, .ho|
|000054a0| 77 65 76 65 72 2c 20 74 | 68 65 20 61 74 6f 6d 69 |wever, t|he atomi|
|000054b0| 63 20 6f 70 65 72 61 74 | 69 6f 6e 61 6c 20 6c 69 |c operat|ional li|
|000054c0| 6d 69 74 20 69 73 20 64 | 65 66 69 6e 65 64 20 69 |mit is d|efined i|
|000054d0| 6e 20 60 60 6c 69 6e 75 | 78 2f 6c 69 6d 69 74 73 |n ``linu|x/limits|
|000054e0| 2e 68 27 27 20 61 73 3a | 0a 0a 5c 62 65 67 69 6e |.h'' as:|..\begin|
|000054f0| 7b 76 65 72 62 61 74 69 | 6d 7d 0a 20 20 20 20 20 |{verbati|m}. |
|00005500| 20 20 20 23 64 65 66 69 | 6e 65 20 50 49 50 45 5f | #defi|ne PIPE_|
|00005510| 42 55 46 20 20 20 20 20 | 20 20 20 34 30 39 36 0a |BUF | 4096.|
|00005520| 5c 65 6e 64 7b 76 65 72 | 62 61 74 69 6d 7d 0a 0a |\end{ver|batim}..|
|00005530| 0a 0a 0a 41 73 20 79 6f | 75 20 63 61 6e 20 73 65 |...As yo|u can se|
|00005540| 65 2c 20 4c 69 6e 75 78 | 20 61 63 63 6f 6d 6d 6f |e, Linux| accommo|
|00005550| 64 61 74 65 73 20 74 68 | 65 20 6d 69 6e 69 6d 75 |dates th|e minimu|
|00005560| 6d 20 6e 75 6d 62 65 72 | 20 6f 66 20 62 79 74 65 |m number| of byte|
|00005570| 73 20 72 65 71 75 69 72 | 65 64 20 62 79 0a 50 4f |s requir|ed by.PO|
|00005580| 53 49 58 2c 20 71 75 69 | 74 65 20 63 6f 6e 73 69 |SIX, qui|te consi|
|00005590| 64 65 72 61 62 6c 79 20 | 49 20 6d 69 67 68 74 20 |derably |I might |
|000055a0| 61 64 64 2e 20 20 54 68 | 65 20 61 74 6f 6d 69 63 |add. Th|e atomic|
|000055b0| 69 74 79 20 6f 66 20 61 | 20 70 69 70 65 20 6f 70 |ity of a| pipe op|
|000055c0| 65 72 61 74 69 6f 6e 0a | 62 65 63 6f 6d 65 73 20 |eration.|becomes |
|000055d0| 69 6d 70 6f 72 74 61 6e | 74 20 77 68 65 6e 20 6d |importan|t when m|
|000055e0| 6f 72 65 20 74 68 61 6e | 20 6f 6e 65 20 70 72 6f |ore than| one pro|
|000055f0| 63 65 73 73 20 69 73 20 | 69 6e 76 6f 6c 76 65 64 |cess is |involved|
|00005600| 20 28 46 49 46 4f 53 29 | 2e 20 20 46 6f 72 0a 65 | (FIFOS)|. For.e|
|00005610| 78 61 6d 70 6c 65 2c 20 | 69 66 20 74 68 65 20 6e |xample, |if the n|
|00005620| 75 6d 62 65 72 20 6f 66 | 20 62 79 74 65 73 20 77 |umber of| bytes w|
|00005630| 72 69 74 74 65 6e 20 74 | 6f 20 61 20 70 69 70 65 |ritten t|o a pipe|
|00005640| 20 65 78 63 65 65 64 73 | 20 74 68 65 20 61 74 6f | exceeds| the ato|
|00005650| 6d 69 63 20 6c 69 6d 69 | 74 0a 66 6f 72 20 61 20 |mic limi|t.for a |
|00005660| 73 69 6e 67 6c 65 20 6f | 70 65 72 61 74 69 6f 6e |single o|peration|
|00005670| 2c 20 61 6e 64 20 6d 75 | 6c 74 69 70 6c 65 20 70 |, and mu|ltiple p|
|00005680| 72 6f 63 65 73 73 65 73 | 20 61 72 65 20 77 72 69 |rocesses| are wri|
|00005690| 74 69 6e 67 20 74 6f 20 | 74 68 65 20 70 69 70 65 |ting to |the pipe|
|000056a0| 2c 0a 74 68 65 20 64 61 | 74 61 20 77 69 6c 6c 20 |,.the da|ta will |
|000056b0| 62 65 20 60 60 69 6e 74 | 65 72 6c 65 61 76 65 64 |be ``int|erleaved|
|000056c0| 27 27 20 6f 72 20 60 60 | 63 68 75 6e 6b 65 64 27 |'' or ``|chunked'|
|000056d0| 27 2e 20 20 49 6e 20 6f | 74 68 65 72 20 77 6f 72 |'. In o|ther wor|
|000056e0| 64 73 2c 20 6f 6e 65 20 | 70 72 6f 63 65 73 73 0a |ds, one |process.|
|000056f0| 6d 61 79 20 69 6e 73 65 | 72 74 20 64 61 74 61 20 |may inse|rt data |
|00005700| 69 6e 74 6f 20 74 68 65 | 20 70 69 70 65 6c 69 6e |into the| pipelin|
|00005710| 65 20 62 65 74 77 65 65 | 6e 20 74 68 65 20 77 72 |e betwee|n the wr|
|00005720| 69 74 65 73 20 6f 66 20 | 61 6e 6f 74 68 65 72 2e |ites of |another.|
|00005730| 0a 0a 0a 0a 0a 5c 73 75 | 62 73 65 63 74 69 6f 6e |.....\su|bsection|
|00005740| 7b 4e 6f 74 65 73 20 6f | 6e 20 68 61 6c 66 2d 64 |{Notes o|n half-d|
|00005750| 75 70 6c 65 78 20 70 69 | 70 65 73 3a 7d 0a 0a 0a |uplex pi|pes:}...|
|00005760| 0a 5c 62 65 67 69 6e 7b | 69 74 65 6d 69 7a 65 7d |.\begin{|itemize}|
|00005770| 0a 5c 69 74 65 6d 20 54 | 77 6f 20 77 61 79 20 70 |.\item T|wo way p|
|00005780| 69 70 65 73 20 63 61 6e | 20 62 65 20 63 72 65 61 |ipes can| be crea|
|00005790| 74 65 64 20 62 79 20 6f | 70 65 6e 69 6e 67 20 75 |ted by o|pening u|
|000057a0| 70 20 74 77 6f 20 70 69 | 70 65 73 2c 20 61 6e 64 |p two pi|pes, and|
|000057b0| 20 70 72 6f 70 65 72 6c | 79 0a 72 65 61 73 73 69 | properl|y.reassi|
|000057c0| 67 6e 69 6e 67 20 74 68 | 65 20 66 69 6c 65 20 64 |gning th|e file d|
|000057d0| 65 73 63 72 69 70 74 6f | 72 73 20 69 6e 20 74 68 |escripto|rs in th|
|000057e0| 65 20 63 68 69 6c 64 20 | 70 72 6f 63 65 73 73 2e |e child |process.|
|000057f0| 0a 5c 69 74 65 6d 20 54 | 68 65 20 70 69 70 65 28 |.\item T|he pipe(|
|00005800| 29 20 63 61 6c 6c 20 6d | 75 73 74 20 62 65 20 6d |) call m|ust be m|
|00005810| 61 64 65 20 42 45 46 4f | 52 45 20 61 20 63 61 6c |ade BEFO|RE a cal|
|00005820| 6c 20 74 6f 20 66 6f 72 | 6b 28 29 2c 20 6f 72 20 |l to for|k(), or |
|00005830| 74 68 65 20 64 65 73 63 | 72 69 70 74 6f 72 73 20 |the desc|riptors |
|00005840| 0a 77 69 6c 6c 20 6e 6f | 74 20 62 65 20 69 6e 68 |.will no|t be inh|
|00005850| 65 72 69 74 65 64 20 62 | 79 20 74 68 65 20 63 68 |erited b|y the ch|
|00005860| 69 6c 64 21 20 20 28 73 | 61 6d 65 20 66 6f 72 20 |ild! (s|ame for |
|00005870| 70 6f 70 65 6e 28 29 29 | 2e 0a 5c 69 74 65 6d 20 |popen())|..\item |
|00005880| 57 69 74 68 20 68 61 6c | 66 2d 64 75 70 6c 65 78 |With hal|f-duplex|
|00005890| 20 70 69 70 65 73 2c 20 | 61 6e 79 20 63 6f 6e 6e | pipes, |any conn|
|000058a0| 65 63 74 65 64 20 70 72 | 6f 63 65 73 73 65 73 20 |ected pr|ocesses |
|000058b0| 6d 75 73 74 20 73 68 61 | 72 65 20 61 20 72 65 6c |must sha|re a rel|
|000058c0| 61 74 65 64 0a 61 6e 63 | 65 73 74 72 79 2e 20 20 |ated.anc|estry. |
|000058d0| 53 69 6e 63 65 20 74 68 | 65 20 70 69 70 65 20 72 |Since th|e pipe r|
|000058e0| 65 73 69 64 65 73 20 77 | 69 74 68 69 6e 20 74 68 |esides w|ithin th|
|000058f0| 65 20 63 6f 6e 66 69 6e | 65 73 20 6f 66 20 74 68 |e confin|es of th|
|00005900| 65 20 6b 65 72 6e 65 6c | 2c 20 61 6e 79 0a 70 72 |e kernel|, any.pr|
|00005910| 6f 63 65 73 73 20 74 68 | 61 74 20 69 73 20 6e 6f |ocess th|at is no|
|00005920| 74 20 69 6e 20 74 68 65 | 20 61 6e 63 65 73 74 72 |t in the| ancestr|
|00005930| 79 20 66 6f 72 20 74 68 | 65 20 63 72 65 61 74 6f |y for th|e creato|
|00005940| 72 20 6f 66 20 74 68 65 | 20 70 69 70 65 20 68 61 |r of the| pipe ha|
|00005950| 73 20 6e 6f 0a 77 61 79 | 20 6f 66 20 61 64 64 72 |s no.way| of addr|
|00005960| 65 73 73 69 6e 67 20 69 | 74 2e 20 20 54 68 69 73 |essing i|t. This|
|00005970| 20 69 73 20 6e 6f 74 20 | 74 68 65 20 63 61 73 65 | is not |the case|
|00005980| 20 77 69 74 68 20 6e 61 | 6d 65 64 20 70 69 70 65 | with na|med pipe|
|00005990| 73 20 28 46 49 46 4f 53 | 29 2e 0a 5c 65 6e 64 7b |s (FIFOS|)..\end{|
|000059a0| 69 74 65 6d 69 7a 65 7d | 0a 0a 0a 0a 0a 0a 5c 73 |itemize}|......\s|
|000059b0| 65 63 74 69 6f 6e 7b 4e | 61 6d 65 64 20 50 69 70 |ection{N|amed Pip|
|000059c0| 65 73 20 28 46 49 46 4f | 73 20 2d 20 46 69 72 73 |es (FIFO|s - Firs|
|000059d0| 74 20 49 6e 20 46 69 72 | 73 74 20 4f 75 74 29 7d |t In Fir|st Out)}|
|000059e0| 0a 0a 0a 0a 0a 5c 73 75 | 62 73 65 63 74 69 6f 6e |.....\su|bsection|
|000059f0| 7b 42 61 73 69 63 20 43 | 6f 6e 63 65 70 74 73 7d |{Basic C|oncepts}|
|00005a00| 0a 0a 41 20 6e 61 6d 65 | 64 20 70 69 70 65 20 77 |..A name|d pipe w|
|00005a10| 6f 72 6b 73 20 6d 75 63 | 68 20 6c 69 6b 65 20 61 |orks muc|h like a|
|00005a20| 20 72 65 67 75 6c 61 72 | 20 70 69 70 65 2c 20 62 | regular| pipe, b|
|00005a30| 75 74 20 64 6f 65 73 20 | 68 61 76 65 20 73 6f 6d |ut does |have som|
|00005a40| 65 20 6e 6f 74 69 63 65 | 61 62 6c 65 0a 64 69 66 |e notice|able.dif|
|00005a50| 66 65 72 65 6e 63 65 73 | 2e 0a 0a 5c 62 65 67 69 |ferences|...\begi|
|00005a60| 6e 7b 69 74 65 6d 69 7a | 65 7d 0a 5c 69 74 65 6d |n{itemiz|e}.\item|
|00005a70| 20 4e 61 6d 65 64 20 70 | 69 70 65 73 20 65 78 69 | Named p|ipes exi|
|00005a80| 73 74 20 61 73 20 61 20 | 64 65 76 69 63 65 20 73 |st as a |device s|
|00005a90| 70 65 63 69 61 6c 20 66 | 69 6c 65 20 69 6e 20 74 |pecial f|ile in t|
|00005aa0| 68 65 20 66 69 6c 65 20 | 73 79 73 74 65 6d 2e 0a |he file |system..|
|00005ab0| 5c 69 74 65 6d 20 50 72 | 6f 63 65 73 73 65 73 20 |\item Pr|ocesses |
|00005ac0| 6f 66 20 64 69 66 66 65 | 72 65 6e 74 20 61 6e 63 |of diffe|rent anc|
|00005ad0| 65 73 74 72 79 20 63 61 | 6e 20 73 68 61 72 65 20 |estry ca|n share |
|00005ae0| 64 61 74 61 20 74 68 72 | 6f 75 67 68 20 61 20 6e |data thr|ough a n|
|00005af0| 61 6d 65 64 20 70 69 70 | 65 2e 0a 5c 69 74 65 6d |amed pip|e..\item|
|00005b00| 20 57 68 65 6e 20 61 6c | 6c 20 49 2f 4f 20 69 73 | When al|l I/O is|
|00005b10| 20 64 6f 6e 65 20 62 79 | 20 73 68 61 72 69 6e 67 | done by| sharing|
|00005b20| 20 70 72 6f 63 65 73 73 | 65 73 2c 20 74 68 65 20 | process|es, the |
|00005b30| 6e 61 6d 65 64 20 70 69 | 70 65 20 72 65 6d 61 69 |named pi|pe remai|
|00005b40| 6e 73 20 69 6e 0a 74 68 | 65 20 66 69 6c 65 20 73 |ns in.th|e file s|
|00005b50| 79 73 74 65 6d 20 66 6f | 72 20 6c 61 74 65 72 20 |ystem fo|r later |
|00005b60| 75 73 65 2e 0a 5c 65 6e | 64 7b 69 74 65 6d 69 7a |use..\en|d{itemiz|
|00005b70| 65 7d 0a 0a 0a 0a 0a 0a | 5c 73 75 62 73 65 63 74 |e}......|\subsect|
|00005b80| 69 6f 6e 7b 43 72 65 61 | 74 69 6e 67 20 61 20 46 |ion{Crea|ting a F|
|00005b90| 49 46 4f 7d 0a 0a 0a 0a | 54 68 65 72 65 20 61 72 |IFO}....|There ar|
|00005ba0| 65 20 73 65 76 65 72 61 | 6c 20 77 61 79 73 20 6f |e severa|l ways o|
|00005bb0| 66 20 63 72 65 61 74 69 | 6e 67 20 61 20 6e 61 6d |f creati|ng a nam|
|00005bc0| 65 64 20 70 69 70 65 2e | 20 20 54 68 65 20 66 69 |ed pipe.| The fi|
|00005bd0| 72 73 74 20 74 77 6f 20 | 63 61 6e 20 62 65 20 64 |rst two |can be d|
|00005be0| 6f 6e 65 0a 64 69 72 65 | 63 74 6c 79 20 66 72 6f |one.dire|ctly fro|
|00005bf0| 6d 20 74 68 65 20 73 68 | 65 6c 6c 2e 0a 0a 5c 62 |m the sh|ell...\b|
|00005c00| 65 67 69 6e 7b 76 65 72 | 62 61 74 69 6d 7d 0a 20 |egin{ver|batim}. |
|00005c10| 20 20 20 20 20 20 20 6d | 6b 6e 6f 64 20 4d 59 46 | m|knod MYF|
|00005c20| 49 46 4f 20 70 0a 20 20 | 20 20 20 20 20 20 6d 6b |IFO p. | mk|
|00005c30| 66 69 66 6f 20 61 3d 72 | 77 20 4d 59 46 49 46 4f |fifo a=r|w MYFIFO|
|00005c40| 0a 5c 65 6e 64 7b 76 65 | 72 62 61 74 69 6d 7d 0a |.\end{ve|rbatim}.|
|00005c50| 0a 0a 0a 0a 54 68 65 20 | 61 62 6f 76 65 20 74 77 |....The |above tw|
|00005c60| 6f 20 63 6f 6d 6d 61 6e | 64 73 20 70 65 72 66 6f |o comman|ds perfo|
|00005c70| 72 6d 20 69 64 65 6e 74 | 69 63 61 6c 20 6f 70 65 |rm ident|ical ope|
|00005c80| 72 61 74 69 6f 6e 73 2c | 20 77 69 74 68 20 6f 6e |rations,| with on|
|00005c90| 65 20 65 78 63 65 70 74 | 69 6f 6e 2e 20 20 54 68 |e except|ion. Th|
|00005ca0| 65 0a 6d 6b 66 69 66 6f | 20 63 6f 6d 6d 61 6e 64 |e.mkfifo| command|
|00005cb0| 20 70 72 6f 76 69 64 65 | 73 20 61 20 68 6f 6f 6b | provide|s a hook|
|00005cc0| 20 66 6f 72 20 61 6c 74 | 65 72 69 6e 67 20 74 68 | for alt|ering th|
|00005cd0| 65 20 70 65 72 6d 69 73 | 73 69 6f 6e 73 20 6f 6e |e permis|sions on|
|00005ce0| 20 74 68 65 20 46 49 46 | 4f 20 66 69 6c 65 0a 64 | the FIF|O file.d|
|00005cf0| 69 72 65 63 74 6c 79 20 | 61 66 74 65 72 20 63 72 |irectly |after cr|
|00005d00| 65 61 74 69 6f 6e 2e 20 | 20 57 69 74 68 20 6d 6b |eation. | With mk|
|00005d10| 6e 6f 64 2c 20 61 20 71 | 75 69 63 6b 20 63 61 6c |nod, a q|uick cal|
|00005d20| 6c 20 74 6f 20 74 68 65 | 20 63 68 6d 6f 64 20 63 |l to the| chmod c|
|00005d30| 6f 6d 6d 61 6e 64 20 77 | 69 6c 6c 0a 62 65 20 6e |ommand w|ill.be n|
|00005d40| 65 63 65 73 73 61 72 79 | 2e 0a 0a 46 49 46 4f 20 |ecessary|...FIFO |
|00005d50| 66 69 6c 65 73 20 63 61 | 6e 20 62 65 20 71 75 69 |files ca|n be qui|
|00005d60| 63 6b 6c 79 20 69 64 65 | 6e 74 69 66 69 65 64 20 |ckly ide|ntified |
|00005d70| 69 6e 20 61 20 70 68 79 | 73 69 63 61 6c 20 66 69 |in a phy|sical fi|
|00005d80| 6c 65 20 73 79 73 74 65 | 6d 20 62 79 20 74 68 65 |le syste|m by the|
|00005d90| 20 60 60 70 27 27 0a 69 | 6e 64 69 63 61 74 6f 72 | ``p''.i|ndicator|
|00005da0| 20 73 65 65 6e 20 68 65 | 72 65 20 69 6e 20 61 20 | seen he|re in a |
|00005db0| 6c 6f 6e 67 20 64 69 72 | 65 63 74 6f 72 79 20 6c |long dir|ectory l|
|00005dc0| 69 73 74 69 6e 67 3a 0a | 0a 5c 62 65 67 69 6e 7b |isting:.|.\begin{|
|00005dd0| 76 65 72 62 61 74 69 6d | 7d 0a 20 20 20 20 20 20 |verbatim|}. |
|00005de0| 20 20 24 20 6c 73 20 2d | 6c 20 4d 59 46 49 46 4f | $ ls -|l MYFIFO|
|00005df0| 0a 20 20 20 20 20 20 20 | 20 70 72 77 2d 72 2d 2d |. | prw-r--|
|00005e00| 72 2d 2d 20 20 20 31 20 | 72 6f 6f 74 20 20 20 20 |r-- 1 |root |
|00005e10| 20 72 6f 6f 74 20 20 20 | 20 20 20 20 20 20 20 20 | root | |
|00005e20| 20 30 20 44 65 63 20 31 | 34 20 32 32 3a 31 35 20 | 0 Dec 1|4 22:15 |
|00005e30| 4d 59 46 49 46 4f 7c 0a | 5c 65 6e 64 7b 76 65 72 |MYFIFO|.|\end{ver|
|00005e40| 62 61 74 69 6d 7d 0a 0a | 0a 0a 0a 41 6c 73 6f 20 |batim}..|...Also |
|00005e50| 6e 6f 74 69 63 65 20 74 | 68 65 20 76 65 72 74 69 |notice t|he verti|
|00005e60| 63 61 6c 20 62 61 72 20 | 28 60 60 70 69 70 65 20 |cal bar |(``pipe |
|00005e70| 73 69 67 6e 27 27 29 20 | 6c 6f 63 61 74 65 64 20 |sign'') |located |
|00005e80| 64 69 72 65 63 74 6c 79 | 20 61 66 74 65 72 20 74 |directly| after t|
|00005e90| 68 65 20 66 69 6c 65 0a | 6e 61 6d 65 2e 20 20 41 |he file.|name. A|
|00005ea0| 6e 6f 74 68 65 72 20 67 | 72 65 61 74 20 72 65 61 |nother g|reat rea|
|00005eb0| 73 6f 6e 20 74 6f 20 72 | 75 6e 20 4c 69 6e 75 78 |son to r|un Linux|
|00005ec0| 2c 20 65 68 3f 0a 0a 54 | 6f 20 63 72 65 61 74 65 |, eh?..T|o create|
|00005ed0| 20 61 20 46 49 46 4f 20 | 69 6e 20 43 2c 20 77 65 | a FIFO |in C, we|
|00005ee0| 20 63 61 6e 20 6d 61 6b | 65 20 75 73 65 20 6f 66 | can mak|e use of|
|00005ef0| 20 74 68 65 20 6d 6b 6e | 6f 64 28 29 20 73 79 73 | the mkn|od() sys|
|00005f00| 74 65 6d 20 63 61 6c 6c | 3a 0a 0a 0a 0a 5c 70 61 |tem call|:....\pa|
|00005f10| 72 0a 5c 61 64 64 76 73 | 70 61 63 65 7b 5c 6d 65 |r.\addvs|pace{\me|
|00005f20| 64 73 6b 69 70 61 6d 6f | 75 6e 74 7d 0a 5c 6e 6f |dskipamo|unt}.\no|
|00005f30| 70 61 67 65 62 72 65 61 | 6b 5c 68 72 75 6c 65 0a |pagebrea|k\hrule.|
|00005f40| 5c 62 65 67 69 6e 7b 76 | 65 72 62 61 74 69 6d 7d |\begin{v|erbatim}|
|00005f50| 0a 20 20 4c 49 42 52 41 | 52 59 20 46 55 4e 43 54 |. LIBRA|RY FUNCT|
|00005f60| 49 4f 4e 3a 20 6d 6b 6e | 6f 64 28 29 3b 20 20 20 |ION: mkn|od(); |
|00005f70| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00005f80| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00005f90| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00005fa0| 20 0a 0a 20 20 50 52 4f | 54 4f 54 59 50 45 3a 20 | .. PRO|TOTYPE: |
|00005fb0| 69 6e 74 20 6d 6b 6e 6f | 64 28 20 63 68 61 72 20 |int mkno|d( char |
|00005fc0| 2a 70 61 74 68 6e 61 6d | 65 2c 20 6d 6f 64 65 5f |*pathnam|e, mode_|
|00005fd0| 74 20 6d 6f 64 65 2c 20 | 64 65 76 5f 74 20 64 65 |t mode, |dev_t de|
|00005fe0| 76 29 3b 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |v); | |
|00005ff0| 20 20 20 0a 20 20 20 20 | 52 45 54 55 52 4e 53 3a | . |RETURNS:|
|00006000| 20 30 20 6f 6e 20 73 75 | 63 63 65 73 73 2c 20 20 | 0 on su|ccess, |
|00006010| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00006020| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00006030| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00006040| 20 20 20 20 0a 20 20 20 | 20 20 20 20 20 20 20 20 | . | |
|00006050| 20 20 2d 31 20 6f 6e 20 | 65 72 72 6f 72 3a 20 65 | -1 on |error: e|
|00006060| 72 72 6e 6f 20 3d 20 45 | 46 41 55 4c 54 20 28 70 |rrno = E|FAULT (p|
|00006070| 61 74 68 6e 61 6d 65 20 | 69 6e 76 61 6c 69 64 29 |athname |invalid)|
|00006080| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00006090| 20 20 20 20 20 0a 20 20 | 20 20 20 20 20 20 20 20 | . | |
|000060a0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000060b0| 20 20 20 20 20 20 20 20 | 45 41 43 43 45 53 20 28 | |EACCES (|
|000060c0| 70 65 72 6d 69 73 73 69 | 6f 6e 20 64 65 6e 69 65 |permissi|on denie|
|000060d0| 64 29 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |d) | |
|000060e0| 20 20 20 20 20 20 0a 20 | 20 20 20 20 20 20 20 20 | . | |
|000060f0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00006100| 20 20 20 20 20 20 20 20 | 20 45 4e 41 4d 45 54 4f | | ENAMETO|
|00006110| 4f 4c 4f 4e 47 20 28 70 | 61 74 68 6e 61 6d 65 20 |OLONG (p|athname |
|00006120| 74 6f 6f 20 6c 6f 6e 67 | 29 20 20 20 20 20 20 20 |too long|) |
|00006130| 20 20 20 20 20 20 20 0a | 20 20 20 20 20 20 20 20 | .| |
|00006140| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00006150| 20 20 20 20 20 20 20 20 | 20 20 45 4e 4f 45 4e 54 | | ENOENT|
|00006160| 20 28 69 6e 76 61 6c 69 | 64 20 70 61 74 68 6e 61 | (invali|d pathna|
|00006170| 6d 65 29 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |me) | |
|00006180| 20 20 20 20 20 20 20 20 | 0a 20 20 20 20 20 20 20 | |. |
|00006190| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000061a0| 20 20 20 20 20 20 20 20 | 20 20 20 45 4e 4f 54 44 | | ENOTD|
|000061b0| 49 52 20 28 69 6e 76 61 | 6c 69 64 20 70 61 74 68 |IR (inva|lid path|
|000061c0| 6e 61 6d 65 29 20 20 20 | 20 20 20 20 20 20 20 20 |name) | |
|000061d0| 20 20 20 20 20 20 20 20 | 20 0a 20 20 20 20 20 20 | | . |
|000061e0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|000061f0| 20 20 20 20 20 20 20 20 | 20 20 20 20 28 73 65 65 | | (see|
|00006200| 20 6d 61 6e 20 70 61 67 | 65 20 66 6f 72 20 6d 6b | man pag|e for mk|
|00006210| 6e 6f 64 20 66 6f 72 20 | 6f 74 68 65 72 73 29 20 |nod for |others) |
|00006220| 20 20 20 20 20 20 20 20 | 20 20 0a 0a 20 20 4e 4f | | .. NO|
|00006230| 54 45 53 3a 20 43 72 65 | 61 74 65 73 20 61 20 66 |TES: Cre|ates a f|
|00006240| 69 6c 65 73 79 73 74 65 | 6d 20 6e 6f 64 65 20 28 |ilesyste|m node (|
|00006250| 66 69 6c 65 2c 20 64 65 | 76 69 63 65 20 66 69 6c |file, de|vice fil|
|00006260| 65 2c 20 6f 72 20 46 49 | 46 4f 29 20 20 20 20 20 |e, or FI|FO) |
|00006270| 20 20 20 20 20 20 20 20 | 20 20 20 20 0a 5c 65 6e | | .\en|
|00006280| 64 7b 76 65 72 62 61 74 | 69 6d 7d 20 0a 5c 6e 6f |d{verbat|im} .\no|
|00006290| 70 61 67 65 62 72 65 61 | 6b 5c 68 72 75 6c 65 20 |pagebrea|k\hrule |
|000062a0| 0a 5c 61 64 64 76 73 70 | 61 63 65 7b 5c 6d 65 64 |.\addvsp|ace{\med|
|000062b0| 73 6b 69 70 61 6d 6f 75 | 6e 74 7d 0a 0a 0a 0a 0a |skipamou|nt}.....|
|000062c0| 49 20 77 69 6c 6c 20 6c | 65 61 76 65 20 61 20 6d |I will l|eave a m|
|000062d0| 6f 72 65 20 64 65 74 61 | 69 6c 65 64 20 64 69 73 |ore deta|iled dis|
|000062e0| 63 75 73 73 69 6f 6e 20 | 6f 66 20 6d 6b 6e 6f 64 |cussion |of mknod|
|000062f0| 28 29 20 74 6f 20 74 68 | 65 20 6d 61 6e 20 70 61 |() to th|e man pa|
|00006300| 67 65 2c 20 62 75 74 20 | 6c 65 74 27 73 0a 63 6f |ge, but |let's.co|
|00006310| 6e 73 69 64 65 72 20 61 | 20 73 69 6d 70 6c 65 20 |nsider a| simple |
|00006320| 65 78 61 6d 70 6c 65 20 | 6f 66 20 46 49 46 4f 20 |example |of FIFO |
|00006330| 63 72 65 61 74 69 6f 6e | 20 66 72 6f 6d 20 43 3a |creation| from C:|
|00006340| 0a 0a 5c 62 65 67 69 6e | 7b 76 65 72 62 61 74 69 |..\begin|{verbati|
|00006350| 6d 7d 0a 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |m}. | |
|00006360| 20 20 20 6d 6b 6e 6f 64 | 28 22 2f 74 6d 70 2f 4d | mknod|("/tmp/M|
|00006370| 59 46 49 46 4f 22 2c 20 | 53 5f 49 46 49 46 4f 7c |YFIFO", |S_IFIFO||
|00006380| 30 36 36 36 2c 20 30 29 | 3b 0a 5c 65 6e 64 7b 76 |0666, 0)|;.\end{v|
|00006390| 65 72 62 61 74 69 6d 7d | 0a 0a 0a 0a 0a 49 6e 20 |erbatim}|.....In |
|000063a0| 74 68 69 73 20 63 61 73 | 65 2c 20 74 68 65 20 66 |this cas|e, the f|
|000063b0| 69 6c 65 20 60 60 2f 74 | 6d 70 2f 4d 59 46 49 46 |ile ``/t|mp/MYFIF|
|000063c0| 4f 27 27 20 69 73 20 63 | 72 65 61 74 65 64 20 61 |O'' is c|reated a|
|000063d0| 73 20 61 20 46 49 46 4f | 20 66 69 6c 65 2e 20 20 |s a FIFO| file. |
|000063e0| 54 68 65 20 72 65 71 75 | 65 73 74 65 64 0a 70 65 |The requ|ested.pe|
|000063f0| 72 6d 69 73 73 69 6f 6e | 73 20 61 72 65 20 60 60 |rmission|s are ``|
+--------+-------------------------+-------------------------+--------+--------+
Only 25.0 KB of data is shown above.